【问题标题】:SLC Loopback: Using model instance from within a model hookSLC Loopback:在模型钩子中使用模型实例
【发布时间】:2015-01-29 21:33:02
【问题描述】:

使用环回模型挂钩,我知道您可以在使用 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();
};

有没有办法获取刚刚创建的用户,或者我是否需要更改我的应用逻辑以使用之前而不是之后?

【问题讨论】:

    标签: node.js model hook loopbackjs strongloop


    【解决方案1】:

    您可以通过“this”访问更新/创建的模型实例:

    User.afterCreate = function(next) {
      var user = this;
      console.log("User created with id: " + user.id)
      next();
    };
    

    【讨论】:

      猜你喜欢
      • 2014-12-25
      • 1970-01-01
      • 1970-01-01
      • 2016-01-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-04-20
      相关资源
      最近更新 更多