在帆中有条件的beforeUpdate

Luj*_*tha 3 sails.js waterline

只有在满足某些条件时才需要加密密码.

beforeUpdate: function (value, cb) {
User.findOne(value.id)
  .exec(function (err, originalUser) {
    if (err || !originalUser) {
      return cb();
    }
    //encrypt the password only if the password is not previously encrypted
    if (value.password != originalUser.password) {
       //encrypt the Pwd
    } else {
      cb();
    }
  });
Run Code Online (Sandbox Code Playgroud)

问题是,值对象只包含更新参数,如何在值对象中获取整个用户对象?

has*_*050 5

更新async后,此方法不再起作用.

你无法获得整个对象beforeUpdate.你可以试试这个来获取id. var id= this.update.arguments[0]

  • 注意 - 由于水线使用的异步变化而不再有效. (4认同)