SLC Loopback:使用模型钩子中的模型实例
使用loopback模型钩子,我知道你可以通过使用beforeCreate创build一个像User一样的模型实例。
User.beforeCreate = function(next, userInstance) { //your logic goes here using userInstance next(); };
但是,如果我需要添加一些使用刚创build的用户的firstName的应用程序逻辑,我该怎么做呢?
User.afterCreate = function(next) { //I need access to the user that was just created and some of it's properties next(); };
有没有一种方法来获得刚刚创build的用户,还是需要更改我的应用程序逻辑以使用之前而不是之后?
您可以通过“this”访问更新/创build的模型实例:
User.afterCreate = function(next) { var user = this; console.log("User created with id: " + user.id) next(); };