我正在尝试通过如下所示的一个调用更新 hasOne
和 belongsTo
模型(顺便说一下,它与 create()
一起工作),其中 user
是返回的实例:
user.update({
email: req.body.email,
Profile: {
name: req.body.name,
gender: req.body.gender,
location: req.body.location,
website: req.body.website,
}
}).then(function(user) { //foo });
但对于我的生活,我无法做任何事情。我可以轻松调用
user.Profile.update({})
并仅更改配置文件模型以及 user.update({})
但嵌套不起作用。要么功能不存在,要么没有很好地记录,要么我需要使用类似于 async.waterfall
的东西分别更新两者?谢谢!
最佳答案
发布此内容以供将来引用,如果有人需要的话。到目前为止,我能弄清楚如何处理这个问题的唯一方法是使用 Promise.all()
。如果 Sequelize 的任何维护者看到这一点,我会非常有助于澄清/添加立即更新相关模型的能力。
这是工作代码(其中 user
是 User.findById()
的一个实例:
Promise.all([
user.Profile.update({
name: req.body.name || '',
gender: req.body.gender || '',
location: req.body.location || '',
website: req.body.website || ''
}),
user.update({
email: req.body.email || '',
})
]).then(function() {
req.flash('success', { msg: 'Profile information updated.' });
res.redirect('/account');
});
关于express - 使用关联将 model.update 序列化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34444242/