【问题标题】:JavaScript Promises not passing right value in ChainJavaScript 承诺不会在链中传递正确的值
【发布时间】:2016-07-22 07:33:48
【问题描述】:

我有一个随机生成的字符串,我将其设置为数据库表中属性的值。在成功更新我的记录后,我会发送一封电子邮件,其中包含在数据库记录中使用的相同令牌。不幸的是,我的 then 语句中的令牌参数不包含令牌,而是被替换为值 1。为什么会发生这种情况,它与 Promise 功能的工作方式有关吗?

这是我的代码中显示的控制台日志和 SQL 更新示例:

This is the token: 78a4543cdd4cfd9d8c7fbad89aed9f902e07c372
Executing (default): UPDATE `user` SET `reset_password_token`='78a4543cdd4cfd9d8c7fbad89aed9f902e07c372',`reset_password_expires`='2016-04-02 14:46:13',`updatedAt`='2016-04-02 13:46:13' WHERE `email` = 'tester@gmail.com'
Then this token: 1

POST 方法:

.post(function(req, res){
        async.waterfall([
        function(done){
            crypto.randomBytes(20, function(err, buf){
                var resetToken = buf.toString('hex');
                done(err, resetToken);
            });
        }, (function(token, done){


            console.log('This is the token: ' + token);


            models.User.update({
                resetPasswordToken: token,
                resetPasswordExpires: Date.now() + 3600000
            }, {
            where: { email: req.body.email }

            }).then(function(token, user, done){


            console.log('Then this token: ' + token);


            var transporter = nodemailer.createTransport(sgTransport(options));

            var mailOptions = {
                from: '"Test Email" <test@mywebsite.com',
                to: 'tester@gmail.com',
                subject: 'Password Rest Confirmation',
                text: 'You are receiving this because you (or someone else) have requested the reset of the password for your account.\n\n' +
                        'Please click on the following link, or paste this into your browser to complete the process:\n\n' +
                        'http://' + req.headers.host + '/reset/' + token + '\n\n' +
                        'If you did not request this, please ignore this email and your password will remain unchanged.\n'
            };

            transporter.sendMail(mailOptions, function(error, info){
                if(error){
                    return console.log(error);
                }
                console.log('Message sent: ' + info.response);

            });
                res.redirect('/');
                //res.redirect('/password-reset-confirmation') Make a confirmation page with information or just send a flash message
            })
        })], function(error){
        if(error){
            console.log(error);
        }
    })  
    });

【问题讨论】:

    标签: javascript node.js promise sequelize.js


    【解决方案1】:

    token 接收值 1,因为 update() 操作返回受影响记录的数量。在这种情况下,更新了一条记录。

    【讨论】:

    • 感谢您的回答。这正是我想要的
    猜你喜欢
    • 1970-01-01
    • 2013-04-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-04-14
    相关资源
    最近更新 更多