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

184
Vistas
ReferenceError: hashPassword is not defined in node.js and bcryct

I'm trying to crypt the password using bcrypt library in node.js and then store in the database but getting this error. I guess the issue is in the return statement as i'm not able to pass the variable outside the callback. I'm a beginner so little bit confused. Any help would be appreciated.

indes.js

const router = require('express').Router();
const bcrypt = require('bcryptjs/dist/bcrypt');
const User = require('../module/user');
// const bcrypt = require('bcryptjs')
const { registerValidation, loginValidation } = require('../validation')
// Validation
// const joi = require('@hapi/joi');



router.post('/register', async (req, res, err) => {

    // Validating data getting form user

    registerValidation(req.body)


    if (error) {
        return res.status(400).send(error.details[0].message)
    }

    // if user in the database
    const emailExist = await User.findOne({ email: req.body.email });
    if (emailExist) {
        return res.status(400).send("Email already exist")
    }

    // Hash password

    bcrypt.hash(req.body.password, 10, function (err, hash) {
        // Store hash in your password DB.
        if (err) {
            return res.send(err, 'In hash')
        }

        // return user.password = hash;

        let hashPassword = hash
        return hashPassword
    });

    // Create new user
    const user = new User({
        name: req.body.name,
        email: req.body.email,
        password: hashPassword
    });

    try {
        const savedUser = await user.save();
        res.send(savedUser);

    } catch (err) {
        res.status(400).send(err);
    }

});

router.get('/test', (req, res, err) => {
    if (err) {
        console.log('Error', err)
    }
    res.send('This is test')


})


module.exports = router;
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