I'm testing out my NodeJs api, and that errors occurs. Here is the code of the function that is not working:
const createUser = (req,res) => {
const name = req.body.name;
const email = req.body.email;
const password = req.body.password;
const newUser = new User({name:name, email:email, password:password});
//----
newUser.save()
.then(() => res.json('200'))
.catch(err => {
if(err.keyPattern.name){
res.json("User already exists");
}else if(err.keyPattern.email){
res.json("Email already exists")
}else{
res.status(400).json("ERROR:"+err);
}
})
}
It is not the folder, because every other route works. I tried to console log everything until the line into I wrote //-----, and everything looks great. Now, The save startement seams to have a problem. Here is the complete error:
(node:12584) UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'name' of undefined
at C:\Users\mposs\OneDrive\Desktop\Files\projects\moondevdotenv\moondev.env\backend1\controller\userController.js:13:31
at processTicksAndRejections (internal/process/task_queues.js:93:5)
(Use `node --trace-warnings ...` to show where the warning was created)
(node:12584) 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:12584) [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.
Can someone help me? Thanks