gpt4 book ai didi

javascript - 在创建 Hook 后更新 sailsjs 模型中的未定义

转载 作者:行者123 更新时间:2023-11-29 19:27:59 25 4
gpt4 key购买 nike

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

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 中的规定进行操作

最佳答案

我认为这种类型的东西应该在您的用户 Controller 中,而不是在模型中。您试图修改您正在定义的模型中定义的模型。

将其移至 Controller 并在 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();
}
}

关于javascript - 在创建 Hook 后更新 sailsjs 模型中的未定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29813403/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com