在创建钩子之后更新 sailsjs 模型中未定义的



在我的 sailsjs 应用程序中,我正在尝试在创建用户后为我的用户添加一个默认头像,所以我在 models/User.js 中有了这个 afterCreate 钩子:

var User = {
  // ...
  attributes: {
    avatar: { model: 'Image' },
    // ...
  },
  afterCreate: function(user, next){
    var url =  sails.config.s3 + "/" + sails.config.default_avatar;
    sails.log.info(user); // This displays the proper user
    var data = { url: url, isAvatar: true, user: user.id };
    Image.create(data) 
    .exec( function (err, image) {
      sails.log.info(image); // This displays the proper image
      User.update(image.user, { avatar: image.id})
      .exec( sails.log.info );
    });
    next();
  },   
}

我不断收到错误:

 /Users/rcanty/Workspace/project/server/api/models/User.js:55
  return User.update(image.user, { avatar: image.id}).exec( sails.log.info
              ^
TypeError: undefined is not a function
    at /Users/rcanty/Workspace/project/server/api/models/User.js:55:19   
at bound (/usr/local/lib/node_modules/sails/node_modules/lodash/dist/lodash.js:957:21)
at applyInOriginalCtx (/usr/local/lib/node_modules/sails/node_modules/waterline/lib/waterline/utils/normalize.js:416:80)
at wrappedCallback (/usr/local/lib/node_modules/sails/node_modules/waterline/lib/waterline/utils/normalize.js:315:18)
at callback.success (/usr/local/lib/node_modules/sails/node_modules/waterline/node_modules/switchback/lib/normalize.js:33:31)
at _switch (/usr/local/lib/node_modules/sails/node_modules/waterline/node_modules/switchback/lib/factory.js:48:28)
at /usr/local/lib/node_modules/sails/node_modules/waterline/lib/waterline/query/dql/create.js:241:9
at /usr/local/lib/node_modules/sails/node_modules/async/lib/async.js:157:25
at bound.<anonymous> (/usr/local/lib/node_modules/sails/node_modules/waterline/lib/waterline/utils/schema.js:151:44)
at fn (/usr/local/lib/node_modules/sails/node_modules/waterline/lib/waterline/utils/callbacksRunner.js:60:10)
at iterate (/usr/local/lib/node_modules/sails/node_modules/async/lib/async.js:149:13)
at Object.async.eachSeries (/usr/local/lib/node_modules/sails/node_modules/async/lib/async.js:165:9)
at Object.runner.afterCreate (/usr/local/lib/node_modules/sails/node_modules/waterline/lib/waterline/utils/callbacksRunner.js:63:9)
at after (/usr/local/lib/node_modules/sails/node_modules/waterline/lib/waterline/query/dql/create.js:236:17)
at /usr/local/lib/node_modules/sails/node_modules/waterline/lib/waterline/query/dql/create.js:223:67
at bound (/usr/local/lib/node_modules/sails/node_modules/lodash/dist/lodash.js:957:21)

但是,似乎我正在做水线文档中规定的操作

我认为这种东西应该在你的用户控制器内,而不是在模型内。 您尝试修改在要定义的模型中定义的模型。

将其移动到控制器并在 createUser 函数结束时调用它。

编辑:

尝试将函数移到属性对象之外,并添加 var User = this; ,以便它可以引用自身:

  var User = {
    // ...
    attributes: {
      avatar: {model: 'Image'},
      // ...
    },
    afterCreate: function(user, next){
      var url =  sails.config.s3 + "/" + sails.config.default_avatar;
      sails.log.info(user); // This displays the proper user
      var data = { url: url, isAvatar: true, user: user.id };
      var User = this; // for internal reference
      Image.create(data)
          .exec( function (err, image) {
            sails.log.info(image); // This displays the proper image
            User.update(image.user, { avatar: image.id})
                .exec( sails.log.info );
          });
      next();
    }
  }

相关内容

  • 没有找到相关文章

最新更新