reactjs - 类型错误 : Cannot read property 'error' of undefined

标签 reactjs next.js

我已经定义了错误。但是,它给出错误“TypeError:无法读取未定义的属性‘错误’”

我这里有货。我将欣赏超越此的想法。

感谢您的帮助。

错误截图

enter image description here

注册组件代码

import React , {useState} from 'react';
import { signup } from '../../../actions/auth';


const SignupComponent = () => {
    const [values, setValues] = useState({
        firstname: '',
        lastname: '',
        email: '',
        phonenumber: '',
        password: '',
        confirmpassword: '',
        howdidyouknowaboutus: '',
        error: '',
        loading: false,
        message: '',
        showForm: true

    });

    const {firstname, lastname, email, phonenumber, password, confirmpassword, howdidyouknowaboutus, error, loading, message, showForm} = values;

    const handleSubmit = e => {
        e.preventDefault();
        /* console.table({firstname, lastname, email, phonenumber, password, confirmpassword,howdidyouknowaboutus, 
            error, loading, message, showForm}); */
        setValues({...values, loading: true, error: false})
        const user = {firstname, lastname, email, phonenumber, password, confirmpassword, howdidyouknowaboutus}

        signup(user).then(data => {
            if (data.error) { ----------------------------------------------- The line with the error
                setValues({...values, error: data.error, loading: false});
            }else{
                setValues({
                    ...values, 
                    firstname: '', 
                    lastname: '', 
                    email: '', 
                    phonenumber: '', 
                    password: '', 
                    confirmpassword: '', 
                    howdidyouknowaboutus: '', 
                    error: '', 
                    loading: false, 
                    message: data.message,
                    showForm: false
                });
            }
        });
    };

   
};

export default SignupComponent;

谢谢

最佳答案

UPD

signup(user).then(data => {
     if (data) { // check here if data not undefined
        if (data.error) {
            setValues({...values, error: data.error, loading: false});
        }else{
            setValues({
                ...values, 
                firstname: '', 
                lastname: '', 
                email: '', 
                phonenumber: '', 
                password: '', 
                confirmpassword: '', 
                howdidyouknowaboutus: '', 
                error: '', 
                loading: false, 
                message: data.message,
                showForm: false
            });
        }
    } else {
         // probably here you would need to do something if dada is undefined
    }
});

上一条回复

更改为

if (data && data.error) 

确保数据已定义

关于reactjs - 类型错误 : Cannot read property 'error' of undefined,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65954490/

相关文章:

javascript - 调用 browserHistory.push ("/url"后 componentDidMount 未运行)

next.js - 我可以让 Next.Js 在构建期间排除 .babelrc 吗?

browser - 频繁从 socket.io 接收数据时 React 崩溃

typescript - 未推断可选泛型类型

next.js - 为什么在NextJS生产阶段没有定义tailwind中的自定义颜色

reactjs - 找不到 'bson' 的类型定义文件。该文件在程序中,因为 : Entry point for implicit type library 'bson'

javascript - ReactJS - 父级更改后子级无法正确重新渲染

javascript - react Material UI 数据网格 : Cannot read property 'useRef' of undefined

css - 新的 tailwindcss 版本 3 中是否需要 CRACO 模块?

javascript - React+gsap 使用随机动画元素组完成后无法工作?