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

190
Visualizações
Password reset not saving to database

My code above runs without any errors but the new password isn't saved.

I've been following the bcrypt docs, a blog post and a video and think the three different sources have resulted in my missing something critical. Any ideas why the new password isn't being saved?

module.exports.submitNewPassword = async (req, res) => {
    const slidedHeaderToken = req.headers.referer.slice(-40);
    const artist = await Artist.find({ resetPasswordToken: slidedHeaderToken, resetPasswordExpires: { $gt: Date.now() } });
    if (!artist) {
        console.log("Artist doesn't exist");
    } else {
        const hashedPassword = async (pw) => {
            bcrypt.hash(req.body.password, 12)
        }
        hashedPassword()
        artist.password = hashedPassword;
        resetPasswordToken = null;
        resetPasswordExpires = null;

        console.log("Successfully resubmitted password");
        res.redirect('login');
    }
}
about 4 years ago · Juan Pablo Isaza
3 Respostas
Responde à pergunta

0

You should call await artist.save() function after you set some fields

about 4 years ago · Juan Pablo Isaza Relatório

0

When you make a change to the data using FindOne, you need to save it.

await yourVariable.save()
about 4 years ago · Juan Pablo Isaza Relatório

0

artists is an array of all the artists matching the parameters in the call to .find(). If you just want to find one, use .findOne().

Then after you modify it, use .save() to save it back to the database.

module.exports.submitNewPassword = async (req, res) => {
    const slidedHeaderToken = req.headers.referer.slice(-40);
    const artist = await Artist.findOne({ resetPasswordToken: slidedHeaderToken, resetPasswordExpires: { $gt: Date.now() } });
    if (!artist) {
        console.log("Artist doesn't exist");
    } else {
        const hashedPassword = async (pw) => {
            bcrypt.hash(req.body.password, 12)
        }
        hashedPassword()
        artist.password = hashedPassword;
        await artist.save();
        resetPasswordToken = null;
        resetPasswordExpires = null;
        console.log("Successfully resubmitted password");
        res.redirect('login');
    }
}

See the Mongoose tutorial here.

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