从 React 向 Mongodb 发送数据时出现 422(无法处理的实体)错误

编程入门 行业动态 更新时间:2024-10-07 16:15:58

从 React 向 Mongodb 发送数据时出现 422(无法处理的<a href=https://www.elefans.com/category/jswz/34/1770371.html style=实体)错误"/>

从 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(无法处理的实体)错误

本文发布于:2024-05-13 16:40:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1760125.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:实体   错误   数据   React   Mongodb

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!