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

149
Visualizações
Where is the result of async function in node app?

So i'm trying to build some basic signup on the backend side with nodejs and MySQL.

let params = [email, password];
    
    const salt = bcrypt.genSaltSync(10);
    var promise = bcrypt.hash(password, salt);
    
    let query = 'insert into users (email, password) values (?,?)';
    connection.promise().query(query, params, (err, result, fields) => {
        console.log(err);
        if(err && err.code === 'ER_DUP_ENTRY') {
            res.status(400).json({
                "message": "There is an account already associated with this email adress!"
            }); 
            res.end();
        }
        else {
            res.status(200).json({
                "message": "User created!"
            });
            res.end();
        }
    });

My problem is when I use the bcrypt.hash function, I dont know how to get the hashed password because the function returns me a promise. So how I access the hashed password so later I can introduce it in my db? I know I can use promise.then() but still don't have an idea about what to do after to get my password. Any ideas, this seems easy but still I am not smart enough

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

You should use await or Use bcrypt.hashSync() method instead of bcrypt.hash(),

const salt = bcrypt.genSaltSync(10);
const hashedPassword = bcrypt.hashSync(password, salt);

//or

const hashedPassword = await bcrypt.hash(password, salt);

Refer Doc: https://www.npmjs.com/package/bcrypt

about 4 years ago · Juan Pablo Isaza Relatório
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