Insert values from a variable into the database with nodejs, mysql and sequelize

Asked

Viewed 689 times

0

I’m trying to input into the database the contents of the variables: token and now. The respective fields in the table users of the bank are: passwordResetToken and passwordResetExpires.

inserir a descrição da imagem aqui

I can return the value of the variables in the console: token and now.

Retorno do valor do token e now

I am trying to save this data in the tables: passwordResetToken and passwordResetExpires.

Making the logic using the Sequelize ORM:

exports.forgotPassword = async (req, res) => {

sgMail.setApiKey(process.env.SENDGRID_API_KEY);

const { email } = req.body;

try {
    const user = await User.findOne({ where: {email} });            //users table
    const token = crypto.randomBytes(20).toString('hex');
    const now = new Date();
    now.setHours(now.getHours() + 1);                             //1 HR valid token



    //error sequelize save datas in db
    await User.findByPk( user.id, {          //error-findByIdAndUpdate--mongo method - users table   { where: {email} } 
        '$set': { 
            passwordResetToken: token,
            passwordResetExpires: now,
        }
    });



    const msg = {
        to: email,
        from: '[email protected]',     //    '[email protected]'
        subject: 'Sending with Twilio SendGrid is Fun',
        text: 'and easy to do anywhere, even with Node.js',
        html: token, email,         //'<strong>and easy to do anywhere fd, even with Node.js {token} </strong> {token}',
    };

    sgMail.send(msg);


    console.log(sgMail);
    console.log(token);
    console.log(now);


    res.status(200).json({

        Success: "Request sent successfully,check token in your email!"

    });

} catch (err) {

    res.status(400).send({ error: 'E-mail does not exist!' });
}

}

Every process is done, but does not save the values of token and now in the fields passwordResetToken and passwordResetExpires on the table users bank. The probable error is here:

//error sequelize save datas in db
    await User.findByPk( user.id, {          //error-findByIdAndUpdate--mongo method - users table   { where: {email} } 
        '$set': { 
            passwordResetToken: token,
            passwordResetExpires: now,
        }
    });

I have no error of reply in the console, however the two fields of the table users are not saved/update. On line 114, this one $set came the way it was done with the mongodb,and functioned normal,now migrating to mysql sequelize,I’m trying to get the data into the database, but still unsuccessful.

Any suggestions?

Thank you all in advance! Thank you!

  • Please edit your question by clicking on "[Edit]" to exchange image codes for text. How you can read here, image code is not a good idea.

  • Done! Could you give me a light for kindness?

1 answer

0


Solved! Just take out the $set and add the sequelize update method.

await user.update({
        passwordResetToken: token,
        passwordResetExpires: now
       })

Browser other questions tagged

You are not signed in. Login or sign up in order to post.