Estoy probando mi API de NodeJs y se producen errores. Aquí está el código de la función que no funciona:
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); } }) } No es la carpeta, porque todas las demás rutas funcionan. Intenté consolar el registro de todo hasta que la línea en la que escribí //----- , y todo se ve muy bien. Ahora, The Save Startement parece tener un problema. Aquí está el error completo:
(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.¿Alguien me puede ayudar? Gracias