Node.js SLC环回:从模型挂钩中使用模型实例

Node.js SLC环回:从模型挂钩中使用模型实例,node.js,model,hook,loopbackjs,strongloop,Node.js,Model,Hook,Loopbackjs,Strongloop,通过使用环回模型挂钩,我知道您可以在使用beforeCreate创建之前访问类似于用户的模型实例: User.beforeCreate = function(next, userInstance) { //your logic goes here using userInstance next(); }; 但是,如果我需要添加一些使用刚刚创建的用户的名字的应用程序逻辑,我该怎么做呢 User.afterCreate = function(next) { //I need acces

通过使用环回模型挂钩,我知道您可以在使用beforeCreate创建之前访问类似于用户的模型实例:

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();
};