2015-12-23 86 views
0

我試圖同時更新hasOnebelongsTo模型通過一個調用,比如下面的(工作,順便說一句,與create()),其中user是返回的實例:Sequelize model.update與關聯

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的東西分別更新它們?

謝謝!

回答

1

發佈此供將來參考,如果有人需要它。到目前爲止,我可以找出如何處理這個問題的唯一方法是使用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'); 
});