0

Buenas me esta enviando un error en esta parte de codigo dice que el error es en linea 29 y la cual corresponde a usuario.password = await bcryptjs.hash(password, salt);

Detallo el codigo en el sisguiente lineas de codigo Como puedo solucionalrlo??

const usuariosPost = async (req = request, res = response) => {
    const { nombre, correo, password } = req.body;
    const usuario = new Usuario({ nombre, correo, password });

    const salt = await bcryptjs.genSalt();
    usuario.password = await bcryptjs.hash(password, salt);

    await usuario.save();

    res.json({
        usuario
    })
}

Y me aparece este error

(node:11616) UnhandledPromiseRejectionWarning: Error: Illegal arguments: undefined, string
    at _async (C:\Users\kenda\Desktop\Restapi\node_modules\bcryptjs\dist\bcrypt.js:214:46)
    at C:\Users\kenda\Desktop\Restapi\node_modules\bcryptjs\dist\bcrypt.js:223:17
    at new Promise (<anonymous>)
    at Object.bcrypt.hash (C:\Users\kenda\Desktop\Restapi\node_modules\bcryptjs\dist\bcrypt.js:222:20)
    at usuariosPost (C:\Users\kenda\Desktop\Restapi\controllers\usuarios.js:29:39)
(node:11616) 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:11616) [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.

1
  • en su: bcryptjs.hash(password, salt) por bcryptjs.hash(password.toString(), salt).
    – ZeroCool
    el 31 may. 2021 a las 3:45

1 respuesta 1

0

te recomiendo no usar async/await en este caso, ya que bcrypt tiene una funcion de callback no bloqueante, que te va a permitir hacer todo directamente, incluido el salt, y guardar el usuario despues.

te propongo cambiar esto:

const salt = await bcryptjs.genSalt();
usuario.password = await bcryptjs.hash(password, salt);

await usuario.save();

res.json({
    usuario
})

por esto:

bcrypt.hash(password, 15, (err, hash)=>{  //donde '15' son las rondas de cifrado haciendo salt automaticamente, como indican en su documentacion. 
  usuario.password = hash;
  usuario.save(); //no necesitas esperar a que el usuario se guarde para enviar la respuesta
                 //a no ser que quieras comprobar que se ha guardado correctamente
  res.json({
   usuario
  })
})

espero te ayude mi respuesta,

saludos.

Tu Respuesta

By clicking “Publica tu respuesta”, you agree to our terms of service and acknowledge you have read our privacy policy.

¿No es la respuesta que buscas? Examina otras preguntas con la etiqueta o formula tu propia pregunta.