Sequelize Update example

There are a couple ways to update values in the Sequelize ORM:

db.Alert.update(
  { url: url },
  { 
    fields: ['url'],
    where: {id: id}
  }
);

If you have an object, this is also supposed to work:

var alert = 
  db.Alert.find(
    {where: {
      id: id,
      UserId: req.user.id
    }});

alert.update({
  url: 'https://www.garysieling.com/blog'
}).then(function() {})

6 Replies to “Sequelize Update example”

    1. Probably because find returns a Promise. It should be more like:
      db.Alert.find(
      {where: {
      id: id,
      UserId: req.user.id
      }}).then(alert => {..})

  1. We need to delete “——activeUsers” : {“ssoid” : “1”,”username” : “LITTA”, “role” : “Engineer”, “projectRole” : null }—–” the json objectt from below json object. Pls help how to do this in sequalize.?

    var json={ “projectName” : “1”,
    “projectId” : “1”,
    “activeUsers” : [
    {
    “ssoid” : “1”,
    “username” : “LITTA”,
    “role” : “Engineer”,
    “projectRole” : null
    },
    {
    “ssoid” : “2”,
    “username” : “SHILPA”,
    “role” : “Admin”,
    “projectRole” : null
    }
    ]}

  2. not working. same sachin: Does not work. TypeError: alert.update is not a function.

    did you check it before publishing it?

      1. But… find would return a promise, wouldn’t it? You can’t just assign the lookup to a variable, you need to chain a .then()

        This doesn’t seem like an API thing

Leave a Reply

Your email address will not be published. Required fields are marked *