SailsJS: beforeUpdate (Lifecycle callback), access current values

与世无争的帅哥 提交于 2019-12-24 03:26:17

问题


It's clear how to access the new values, the first argument contains them

beforeUpdate: function (values, cb) {....}

But how can I access the current values, that are to be replaced?


回答1:


In order do access current values you will have to make the call and get the current record.

models/user.js
beforeCreate: function (values, cb) {
    User.findOne(this.update.arguments[0].id || this.update.arguments[0]).exec(err, function(originalUser){/*...*/})
}

Also check out https://github.com/balderdashy/waterline/issues/1004#issuecomment-102432862 , where this issue is being discussed. Should provide insight into getting the ID and a current solution.

The solution above was updated to reflect the comments.



来源:https://stackoverflow.com/questions/30403501/sailsjs-beforeupdate-lifecycle-callback-access-current-values

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!