如何使用 SailsJS Waterline update() 方法将数据添加到集合行中的两个字段

How to add data to two fields in a collection row with SailsJS Waterline update() method

我现在正在为我的应用程序创建一个忘记密码功能,我正在努力的一个关键步骤是在用户开始忘记密码过程时将数据添加到两个与忘记密码相关的字段。

字段是

1) resetPasswordToken - 随机生成的令牌,可暂时允许用户设置新密码。

2) resetPasswordExpires - 从创建之日起整整一小时的日期,它对密码可以更改的时间设置了时间限制。

我到了需要用这两个字段更新用户行(对于需要更改密码的用户!)的部分,但我不知道该怎么做。我正在尝试使用 SailsJS Update method 来完成此操作。在我使用 update() 方法更新模型之前,我可以使用一些标准符号来更新模型,例如:

User.update(req.param('id'), req.body)
    .exec(function (err, users) {
        if (err) { return res.json(400, err); }
        res.json(users[0]);
    });

这很容易,因为 1. 我一次更新一整行,并且 2. 因此,我有 id 我可以通过。更新整行是使用我发现有据可查的更新的唯一方法。

然而,现在要困难得多。我只需要更新两个字段,而且我认为在执行此服务器端操作时我没有可用的 id。文档没有指定如何做这样的事情。我有他们用来注册的 email 地址, 唯一的,所以应该可以。我想我需要使用某种 .where() 吗?

此时整个服务器端操作看起来有点像这样:

forgot: function (req, res, next) {
    async.waterfall([
        function(done) {
            crypto.randomBytes(20, function(err, buf) {
                var token = buf.toString('hex');
                done(err, token);
            });
        },
        function(token, done) {
            User.findOne({ email: req.body.email }, function(err, user) {
                if (!user) {
                    done(null, false, { message: 'Email doesn't exist' });
                }

                user.resetPasswordToken = token;
                user.resetPasswordExpires = Date.now() + 3600000;
                // TODO: this part will have to be figured out
                // Have no idea how to update correctly! Left it blank

                User.update( );

                // Above is the key problem
                done(err, token, user);
            });
        },
        function(token, user, done) {
            htmlMessage = '<h4>You are receiving this because you (or someone else) have requested the reset of the password for your account.</h4>' +
                            '<p>Please click on the following link, or paste this into your browser to complete the process:</p>' +
                            "<p><a href='http://" + req.headers.host + '/reset/' + token + "'> Link to password reset </a></p>" +
                            '<p>If you did not request this, please ignore this email and your password will remain unchanged.</p>';

            var emailInfo = {
                to: user.email,
                from: "customerservice@work.com",
                subject: "Password Reset",
                message: htmlMessage,
                fromName: "Random Organization"
            };

            EmailService.simpleSendEmail(emailInfo, function (err) {
                done(err, 'done');
            });
        }
        ], function(err) {
            if (err) return next(err);
            res.redirect('/login');
        });
},

你快到了。在获取 User 实例并根据需要更改属性后,无需调用 User.update(),只需在实例上调用 save()

User.findOne({/* criteria */}).exec(function(err, user){
    if (err) handleErrors(err);
    user.property = newValue;//and any other changes you need
    user.save(function(err, savedRecord){
        cb(err, savedRecord);
    });
});

为避免进行两次数据库调用,您可以这样使用 update() 方法:

User.update({ email: userEmail },
            { resetPasswordToken: newToken,
              resetPasswordExpires: newExpires })
          .exec( function(err, updatedObjArray) {
                return callback(err, updatedObjArray);
               // Note that an array will be returned even
               // if only a single record has been updated.
});

你写的大部分代码都是正确的,你只需要改变说

的那一行
User.update( );
done(err, token, user);

user.save(function(err){
   done(err, token, user);
});

或者按照@galactocalypse 的建议,您可以直接使用更新查询。