reactjs - 未捕获(承诺中)TypeError:无法读取未定义的属性“错误”
问题描述
Register
我在组件中收到以下错误handleSubmit
:
Uncaught (in promise) TypeError: Cannot read property 'error' of undefined.
您可以在组件下方找到我如何获取 API 数据以及 Next.js 配置。
注册.js
import React,{useState} from 'react';
import {signup} from "../../actions/authentication";
const Register = () => {
const [info,setInfo] = useState({name:"",email:"",password:"",error:"",loading:false,message:"",showForm:true});
const {name,email,password,error,loading,message,showForm} = info
const handleSubmit = event => {
event.preventDefault();
// console.table({ name, email, password, error, loading, message, showForm });
setInfo({ ...info, loading: true, error: false });
const user = { name, email, password };
signup(user).then(data => {
if (data.error) {
`^^^^^^^^^^`
` Uncaught (in promise) TypeError: Cannot read property 'error' of undefined.`
setInfo({ ...info, error: data.error, loading: false });
} else {
setInfo({...info,name: '',email: '',password: '',error: '',loading: false,message: data.message,showForm: false});
}
});
};
const handleChange= name =>(event)=>{
setInfo({...info,error:false,[name]: event.target.value});
}
const registerForm=()=>{
return (
<form onSubmit={handleSubmit}>
{/* Name */}
<div className="form-group">
{/* dynamic handle change by passing the value straight to it */}
<input type="text" value={name} className="form-control" onChange={handleChange("name")} placeholder="Enter Your Name"/>
</div>
{/* Email */}
<div className="form-group">
<input type="email" value={email} className="form-control" onChange={handleChange("email")} placeholder="Enter Your @Email address"/>
</div>
{/* Password */}
<div className="form-group">
<input type="password" value={password} className="form-control" onChange={handleChange("password")} placeholder="Enter Your password"/>
</div>
<button className="btn btn-info">Register</button>
</form>
)}
return (
<>
{registerForm()}
</>
)
}
export default Register;
身份验证.js
import fetch from "isomorphic-fetch";
import {API} from "../config.js";
export const signup = (user) => {
return fetch(`${API}/signup`, {
method: 'POST',
headers: {
Accept: 'application/json',
'Content-Type': 'application/json'
},
body: JSON.stringify(user)
})
.then(response => {
return response.json();
})
.catch(err => console.log(err));
};
配置.js
import getConfig from "next/config";
const {publicRuntimeConfig} = getConfig();
export const API = publicRuntimeConfig.PRODUCTION ? publicRuntimeConfig.PRODUCTION_SITE : publicRuntimeConfig.DEVELOPMENT_SITE ;
export const APP_NAME = publicRuntimeConfig.APP_NAME;
next.config.js
module.exports = {
publicRuntimeConfig:{
APP_NAME: "----------",
API:"http://localhost:4000/api",
PRODUCTION:false,
PRODUCTION_SITE:"https://---------.netlify.app",
DEVELOPMENT_SITE:"http://localhost:3000",
}
};
解决方案
解决了它:
next.config.js
module.exports = {
publicRuntimeConfig:{
APP_NAME: "----------",
API:"http://localhost:4000/api",
PRODUCTION:false,
PRODUCTION_SITE:"https://---------.netlify.app",
DEVELOPMENT_SITE:"http://localhost:4000",
}
};
身份验证.js
import fetch from "isomorphic-fetch";
import {API} from "../config.js";
export const signup = (user) => {
return fetch(`${API}/api/signup`, {
method: 'POST',
headers: {
Accept: 'application/json',
'Content-Type': 'application/json'
},
body: JSON.stringify(user)
})
.then(response => {
return response.json();
})
.catch(error => console.log(error));
};
推荐阅读
- java - 调试 ClassCastException
- kotlin - Kotlin 的 Intelij 更改等于和哈希码模板
- java - 如何在执行后终止 do-while 循环?
- java - 未调用单元测试服务方法
- c++ - 为什么自空函数测量以来的时间有几纳秒的误差?
- python - Pygame - 精灵组运动不起作用
- java - Ubuntu 16.04 的 Java 11 安装问题
- excel - 在 vba 中寻找更有效的算法来评估指数增长的组合问题
- json - 如何解析json字符串或检查批处理脚本中json中是否存在字符串
- c++ - CDockablePane 中下拉组中的 MFC 对话框设置