简体   繁体   English

JavaScript承诺不会在Chain中传递正确的值

[英]JavaScript Promises not passing right value in Chain

I have a random generated string that I set as a value for a property in my database table. 我有一个随机生成的字符串,可以将其设置为数据库表中属性的值。 On a successful update to my record I then send an email containing that same token that was used in the database record. 成功更新记录后,我将发送一封电子邮件,其中包含与数据库记录相同的令牌。 Unfortunately the token parameter in my then statement does not contain the token and instead is replaced with a value of 1. Why is this happening and does it have something to do with how promises functionality works? 不幸的是,我的then语句中的token参数不包含令牌,而是被值1代替。为什么会发生这种情况,并且它与promise功能的工作方式有关系吗?

This is an example console log and SQL update that appears in my code: 这是示例控制台日志和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 Method: 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);
        }
    })  
    });

The token receives the value 1 because the update() operation returns the number of affected records . token收到值1因为update()操作返回受影响的记录数 In this case, there was a single record updated. 在这种情况下,只有一条记录已更新。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM