【问题标题】:field in mongoose model is required : true but it is being created in postman猫鼬模型中的字段是必需的:是的,但它是在邮递员中创建的
【发布时间】:2020-09-19 23:59:52
【问题描述】:

我试图在正文中只给出名称,并希望在邮递员中出错......但是邮递员中的状态响应是 201 创建的,但它在控制台中抛出错误

UnhandledPromiseRejectionWarning:ValidationError:用户验证失败:密码:路径password 是必需的。电子邮件:路径email 是必需的。 在 model.Document.invalidate (C:\projects\MERN\backend\node_modules\mongoose\lib\document.js:2564:32) 在 C:\projects\MERN\backend\node_modules\mongoose\lib\document.js:2386:17 在 C:\projects\MERN\backend\node_modules\mongoose\lib\schematype.js:1181:9 在 processTicksAndRejections (internal/process/task_queues.js:79:11) (节点:6524)UnhandledPromiseRejectionWarning:未处理的承诺拒绝。此错误源于在没有 catch 块的情况下抛出异步函数内部,或拒绝未使用 .catch() 处理的承诺。要在未处理的 Promise 拒绝时终止节点进程,请使用 CLI 标志 --unhandled-rejections=strict(请参阅 https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode)。 (拒绝编号:1) (节点:6524)[DEP0018] DeprecationWarning:不推荐使用未处理的承诺拒绝。将来,未处理的 Promise 拒绝将使用非零退出代码终止 Node.js 进程。

为什么邮递员没有错误???????????

const mongoose = require('mongoose')


const userSchema = new mongoose.Schema({
name:{
    type : String,
    required : true
},
email:{
    type : String,
    required : true,
    unique:true,
},
password:{
    type : String,
    required : true,
    minlength: 7
},
date:{
    type :Date,
    default: Date.now
}

})

 const User = mongoose.model('User',userSchema)

 module.exports = User

router.post("/", async (req, res) => {
try {
const user = await new User(req.body);
user.save();
res.status(201).send({user});
} catch (e) {
res.status(500).send(e);
}

});

【问题讨论】:

    标签: javascript node.js mongoose postman


    【解决方案1】:

    考虑到您的节点应用程序正确地抛出了一些错误并崩溃,正如您在上面所描述的那样。因为您的节点应用程序正在与互联网连接,您需要设计一种方法来将您的应用程序中的错误解释为互联网也已知的错误,这样邮递员就能够判断发生了错误......那么我们如何实现这一点,答案是错误处理……

    我们将使用您所描述的User 模型,并考虑它下面的代码...

    router.post("/", async (req, res) => {
      try {
        const user = await new User(req.body);
    
        // One important thing to note is that the return of this function call below is
        // a Promise object which means that it executes asynchrounously and from the error
        // log you have above, it is the reason your app is crashing...
        user.save();
        res.status(201).send({user});
      } catch (e) {
        res.status(500).send(e);
      }
    });
    

    那么让我们修复它...

    router.post("/", async (req, res) => {
      const user = await new User(req.body);
      return user.save()
    
      // the then call simply accepts a callback that is executed after the async is complete
      .then((result) => res.status(201).send({user}))
    
      // this catch will be called in case the call encounters an error during execution
      .catch((error) => res.status(500).send(error));
    });
    

    请注意,现在我们通过响应 HTTP 请求来处理 catch 中的错误,就像您使用 500 代码一样......并且还将错误与响应一起发送

    【讨论】:

      猜你喜欢
      • 2017-11-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-06-05
      • 1970-01-01
      • 2019-02-04
      • 2015-09-16
      • 1970-01-01
      相关资源
      最近更新 更多