通过环回模型 Hook ,我知道您可以在使用 beforeCreate 创建模型之前访问 User 等模型的实例:
User.beforeCreate = function(next, userInstance) {
//your logic goes here using userInstance
next();
};
但是,如果我需要添加一些使用刚刚创建的用户的名字的应用程序逻辑,我该怎么做?
User.afterCreate = function(next) {
//I need access to the user that was just created and some of it's properties
next();
};
有没有办法获取刚刚创建的用户,或者我是否需要更改我的应用程序逻辑以在之前而不是之后使用?
最佳答案
您可以通过“this”访问更新/创建的模型实例:
User.afterCreate = function(next) {
var user = this;
console.log("User created with id: " + user.id)
next();
};
关于node.js - SLC环回: Using model instance from within a model hook,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27235838/