Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Update Sequelize Array

I have looked everywhere, and I have not found an easy to understand method of updating a sequelize array, like this with a normal string:

db.User.update({name:req.body.name},{where : {username:req.body.username}}).then(function(user) {

    res.json(user);
})
like image 302
Danny McClafferty Avatar asked Mar 13 '23 02:03

Danny McClafferty


1 Answers

Sequelize doesn't support bulk updates using an array, see https://github.com/sequelize/sequelize/issues/4501

You have to implement a custom function. Here is a basic example to give you an idea :

var promises = [];
userArray.forEach(function(user){
   promises.push(db.User.update({name:user.name},{where : {username:user.username}});
});
Promise.all(promises).then(function(){
    // success
}, function(err){
    // error
});
like image 148
Raptack Avatar answered Mar 20 '23 08:03

Raptack