Reputation: 75
I'm Pretty new doing this kind of tasks in Node. I have a POST method (express) which receives an array of objects like:
[{F1:'123',F2:'a',F3:'b',F4:'Z'},
{F1:'124',F2:'a',F3:'b',F4:'Z'},
{F1:'125',F2:'a',F3:'b',F4:'Z'},
{F1:'126',F2:'a',F3:'b',F4:'Z'},]
Then, I need do an Update for every object in the array. I'm using sequelize:
MODEL.update(
{
att4: art['F4'],
},
{
where:{
att1: {$eq: art['F1']}
}
}).then(function(result)
{
res.status(200).send();
}).catch(function(err)
{
res.status(500).send(err);
})
And this work for 1 object.
But I need the following: WHEN ALL THE UPDATES are processed, then send a response.
I tried with
req.body.forEach(function(o)
{
updateO(o)
},this);
and in updateO(o) do the Model.Update, but I don't achieve the needed result.
Sorry for the bad English, I hope you can understand me and thanks for your help.
Upvotes: 0
Views: 1970
Reputation: 2775
Read about promises, all Sequelize's functions return promises, you can handle multiple querying through Promise.all
var objects = req.body;
Promise.all(objects.map(object=>{
return MODEL.update({att4: object.F4},{where:{att1: object.F1});
})).then(function(result){
res.status(200).send();
}).catch(function(err){
res.status(500).send(err);
})
Upvotes: 1