Keith
Keith

Reputation: 2009

Return object with updated association in Sequelize

Using Sequelize and MySQL, updating an object including the associated object it has. Everything updates fine but I can't get the new associated object to return. If I do another GET request it's the new one but I need it to come back on the response after an update.

I'm trying to just reload that contact object before return.

The object looks like this:

{

    "id": 1,

    "details": "some task details",

        "contact": { //associated object

            "associatedId": 1,

            "name": "Mike",

       }

}

This is what I'm trying

db.task.findOne({
  where: {
    id: taskId,
    userId: req.user.get('id')
  },
  include: [db.contact]
}).then(
  function(task) {
    if (task) {
      return task.update(attributes);
    } else {
      res.status(404).send();
    }
  },
  function() {
    res.status(500).send();
  }
).then(
  function(task) {
    if(task) {
      res.json(task);
    }
  },
  function(e) {
    res.status(400).json(e);
  }
);

Upvotes: 1

Views: 1476

Answers (1)

Vivek Doshi
Vivek Doshi

Reputation: 58603

All you need to do is returning: true :

return task.update(attributes,{
  returning: true,
  plain: true
});

Upvotes: 3

Related Questions