我有 Students
和 Classes
,它们之间有 hasMany 关联。
我这样做:
myStudent.setClasses(ids). then(function(result) {
console.log(myStudent.Classes);
});
问题:
result
参数在 then-handler 中是什么意思? myStudent.Classes
没有与我所做的 setClasses()
更改保持同步? Classes
成员?我需要向调用者返回一个简单的 JSON 响应。 最佳答案
result
方法时, Classes
将是关联的 .setClasses
(在你的情况下)。因此,您的
ids
参数实际上应该是 Classes
,也许您应该在此之前要求它们Class.findAll({where: {id: ids}})
.on('success', function (classes) {
myStudent.setClasses(classes)
.on('success', function (newAssociations) {
// here now you will have the new classes you introduced into myStudent
// you say you need to return a JSON response, maybe you could send this new associations
})
})
myStudent
)。您应该在现有的 result
数组中添加新关联( newAssociations
var,在您的示例中为 myStudent.Classes
,在我的示例中)。也许 reloading 您的实例也应该可以工作。Class.findAll({where: {id: ids}})
.on('success', function (classes) {
myStudent.setClasses(classes)
.on('success', function (newAssociations) {
myStudent.Classes = myStudent.Classes || [];
myStudent.Classes.push(newAssociations);
// guessing you're not using that myStudent obj anymore
res.send(myStudent);
})
})
Classes
成员来解释你的意思吗? 关于node.js - Sequelize : setter for association does not update simple member for it,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28044445/