在我sailsjs的應用程序,我想添加一個默認的頭像我的用戶在創建後的用戶,所以我必須在此models/User.js
鉤afterCreate:更新在sailsjs模型未定義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)
但是它好像我做什麼公司在waterline docs
你創建這個還是你帶帆產生的呢?我沒有看到你在哪裏出口這個 – tpie
我在sails-auth-generate – onetwopunch