JavaScript Promises 未在链中传递正确的值

标签 javascript node.js promise sequelize.js

我有一个随机生成的字符串,我将其设置为数据库表中的属性值。成功更新我的记录后,我会发送一封电子邮件,其中包含数据库记录中使用的相同 token 。不幸的是,我的 then 语句中的 token 参数不包含 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()操作返回受影响记录的数量。在本例中,更新了一条记录。

关于JavaScript Promises 未在链中传递正确的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36374200/

相关文章:

javascript - Google Analytics 的这个正则表达式实际上有什么作用吗?

javascript - 如何测试 Quasar(作为 Vue CLI 插件)?

json - 将 JSON 从模型传递到路由不起作用

javascript - 当 Promise 永远不会解决时会发生什么?

javascript - 将回调转换为 Promise

javascript - 可以使用纯文本文件进行身份验证吗?

javascript点击被调用两次或更多

javascript - 使用 Minecraft 控制台和 Nodejs 进行正则表达式

javascript - 在 Node.js 中下载 typescript 中的文件

AngularJS - 由于 Promise 对象而导致无限循环