我试图连接前端登录页面到后端mongodb。我得到如下所述的错误。此代码从前端获取数据,并发送后端的注册标记。
name: "",
email: "",
});
let name, value;
const handleInputs = (e) => {
console.log(e);
name = e.target.name;
value = e.target.value;
setuser({ ...user, [name]: value });
};
const PostData = async (e) => {
e.preventDefault();
// http://localhost:3002/register
const [name, email] = user;
console.log(name);
console.log(email);
const res = await fetch("/register", {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({
name,
email,
}),
});
const data = await res.json();
console.log(name);
console.log(email);
if (data.status === 422 || !data) {
window.alert("Invalid Registeration");
} else {
window.alert(" Registeration Successfull");
}
};
错误信息:
Uncaught (in promise) TypeError: user is not iterable at PostData (App.js:30:1) at HTMLUnknownElement.callCallback (react-dom.development.js:4164:1) at Object.invokeGuardedCallbackDev (react-dom.development.js:4213:1) at invokeGuardedCallback (react-dom.development.js:4277:1) at invokeGuardedCallbackAndCatchFirstError (react-dom.development.js:4291:1) at executeDispatch (react-dom.development.js:9041:1) at processDispatchQueueItemsInOrder (react-dom.development.js:9073:1)
at processDispatchQueue (react-dom.development.js:9086:1) at dispatchEventsForPlugins (react-dom.development.js:9097:1) at react-dom.development.js:9288:1
1条答案
按热度按时间ryhaxcpt1#
看起来用户对象有问题,你可以尝试记录它,看看它在每一步给出了什么,尝试JSON.parse(),我还可以看到你传递名称,电子邮件到JSON.strigfy(),而不是尝试另一种方式,并以这种方式解构const { name,email } = user not const [name,email ] = user ;