【发布时间】:2020-03-06 11:39:30
【问题描述】:
我正在使用 Node.js 在服务器上执行验证并将响应发送回客户端
try {
const response = await register(user);
console.log(response);
} catch (ex) {
if (ex.response && ex.response.status === 422) {
const errors = ex.response.data.errors;
this.setState({ errors });
}
}
如果我 console.log(this.state.errors) 我得到:
(5) [{…}, {…}, {…}, {…}, {…}]
0: {name: "Name required"}
1: {name: "Name should be more than 3 characters"}
2: {email: "Invalid email address"}
3: {password: "Password required"}
4: {password: "Password should be at least 6 characters"}
length: 5
__proto__: Array(0)
然后我想遍历所有错误并将它们显示在一个下方。
{this.state.errors &&
this.state.errors.map(error => <li>{error}</li>)}
但我收到一条错误消息:
this.state.errors.map 不是函数
【问题讨论】:
标签: reactjs