【问题标题】:Sequelize model.update with associationSequelize model.update 与关联
【发布时间】:2015-12-24 06:59:16
【问题描述】:

我正在尝试通过一个调用来更新hasOnebelongsTo 模型,如下所示(顺便说一句,与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 的东西分别更新两者?

谢谢!

【问题讨论】:

    标签: express orm sequelize.js


    【解决方案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');
    });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-06-11
      • 2012-11-24
      • 2016-11-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多