在我的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)
然而,我似乎完全按照waterline docs
的规定行事答案 0 :(得分:2)
I think this type of thing should be inside your User controller, not inside the model. Your trying to modify a model that you are defining inside the model you are defining.
Move it to the controller and call it at the end of your createUser function.
EDIT:
Try moving the function outside the attributes object, and add var User = this;
, so that it can reference itself:
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();
}
}