简体   繁体   中英

field in mongoose model is required : true but it is being created in postman

i am trying to give only name in the body and want error in the postman...but for the status response in postman is 201 created but it is throwing error in console as

UnhandledPromiseRejectionWarning: ValidationError: User validation failed: password: Path password is required., email: Path email is required. at model.Document.invalidate (C:\projects\MERN\backend\node_modules\mongoose\lib\document.js:2564:32) at C:\projects\MERN\backend\node_modules\mongoose\lib\document.js:2386:17 at C:\projects\MERN\backend\node_modules\mongoose\lib\schematype.js:1181:9 at processTicksAndRejections (internal/process/task_queues.js:79:11) (node:6524) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with.catch(). To terminate the node process on unhandled promise rejection, use the CLI flag --unhandled-rejections=strict (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode ). (rejection id: 1) (node:6524) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

why there is no error in postman???????????

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);
}

});

consider that your node application throws some error right and crashes as you describe well above. Because your node app is interfacing with the internet you need to devise a way to interpret the error from you app into to an error that is known by the internet also, that way postman will be able to tell that an error has occured...So how do we achieve this, the answer is error handling...

We will use your User model as you have described, and consider the code below it...

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);
  }
});

So then lets fix it...

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));
});

Note now we handle the error in the catch by responding to the HTTP request as you have with a 500 code...and also sending the error along with the response

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM