簡體   English   中英

JavaScript承諾不會在Chain中傳遞正確的值

[英]JavaScript Promises not passing right value in Chain

我有一個隨機生成的字符串,可以將其設置為數據庫表中屬性的值。 成功更新記錄后,我將發送一封電子郵件,其中包含與數據庫記錄相同的令牌。 不幸的是,我的then語句中的token參數不包含令牌,而是被值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);
        }
    })  
    });

token收到值1因為update()操作返回受影響的記錄數 在這種情況下,只有一條記錄已更新。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM