Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

198
Vistas
UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'name' of undefined, in my api

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

about 4 years ago · Juan Pablo Isaza
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda