Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

185
Visualizações
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 à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda