【发布时间】:2017-04-12 06:07:26
【问题描述】:
Sail 多对多关联在添加关联对象后在保存时抛出错误。这是我的建模。
User:
module.exports = {
attributes: {
...
operations: {
collection: 'operation',
via: 'users'
}
}
};
操作:
module.exports = {
attributes: {
...
users: {
collection: 'users',
via: 'operations',
}
}
};
在我用户的控制器上,我有:
addPermissionToUsers: function(req, res) {
Users.findOne(2).populate('operations').exec(function (err, user) {
if (err) throw err// handle error
// Queue up a record to be inserted into the join table
user.operations.add(1);
Save the user, creating the new associations in the join table
user.save(function (err) {
if (err) throw err
res.json(user)
});
});
}
这是错误跟踪:
if (err) throw err
^
Error (E_UNKNOWN) :: Encountered an unexpected error
at new WLError (/usr/local/lib/node_modules/sails/node_modules/waterline/lib/waterline/error/WLError.js:25:15)
at /usr/local/lib/node_modules/sails/node_modules/waterline/lib/waterline/model/lib/defaultMethods/save.js:188:17
at /usr/local/lib/node_modules/sails/node_modules/waterline/node_modules/async/lib/async.js:52:16
at /usr/local/lib/node_modules/sails/node_modules/waterline/node_modules/async/lib/async.js:550:17
at /usr/local/lib/node_modules/sails/node_modules/waterline/node_modules/async/lib/async.js:544:17
at _arrayEach (/usr/local/lib/node_modules/sails/node_modules/waterline/node_modules/async/lib/async.js:85
【问题讨论】:
-
你也有
Users模型吗? -
是的,我有一个用户。
-
不是
User,你有Users型号吗?此行 (collection: 'users',) 指的是Users模型,而不是User。
标签: sails.js waterline sails-postgresql