实体)错误"/>
从 React 向 Mongodb 发送数据时出现 422(无法处理的实体)错误
当我通过 Node.js 将表单数据从 React 发送到 Mongo DB 时出现
422 (Unprocessable Entity)
错误
(这是一个 MERN 项目)
错误看起来像
SignUp.jsx:24 POST http://localhost:3000/signup 422 (Unprocessable Entity)
registerData @ SignUp.jsx:24
callCallback @ react-dom.development.js:4164
invokeGuardedCallbackDev @ react-dom.development.js:4213
invokeGuardedCallback @ react-dom.development.js:4277
invokeGuardedCallbackAndCatchFirstError @ react-dom.development.js:4291
executeDispatch @ react-dom.development.js:9041
processDispatchQueueItemsInOrder @ react-dom.development.js:9073
processDispatchQueue @ react-dom.development.js:9086
dispatchEventsForPlugins @ react-dom.development.js:9097
(anonymous) @ react-dom.development.js:9288
batchedUpdates$1 @ react-dom.development.js:26140
batchedUpdates @ react-dom.development.js:3991
dispatchEventForPluginEventSystem @ react-dom.development.js:9287
dispatchEventWithEnableCapturePhaseSelectiveHydrationWithoutDiscreteEventReplay @ react-dom.development.js:6465
dispatchEvent @ react-dom.development.js:6457
dispatchDiscreteEvent @ react-dom.development.js:6430
虽然我试过了
"proxy": {
"/signup": {
"target": "http://localhost:5000",
"secure": false
}
},
(server is running on 5000 and client is on 3000)
还是不行? 这是发送数据的代码
const registerData = async (e) => {
e.preventDefault();
const { name, email, phone, work, password, cpassword } = user;
const response = await fetch('/signup', {
method: "POST",
headers: {
"Content-Type": "application/json"
},
body: JSON.stringify({
name, email, phone, work, password, cpassword
})
});
const data = await response.json();
if (data.status === 422 || !data) {
window.alert("Invalid Registration");
console.log("Invalid Registration");
}
else {
window.alert("Registration Successful");
console.log("Registration Successful");
}
};
单击此处 在 GitHub 上显示完整项目。
我想成功地将表单数据发送到数据库并注册用户然后使用该凭据登录
回答如下:更多推荐
从 React 向 Mongodb 发送数据时出现 422(无法处理的实体)错误
发布评论