dresende / node-orm2

Object Relational Mapping
http://github.com/dresende/node-orm2
MIT License
3.07k stars 376 forks source link

General CallBack using each #508

Closed nicolasroger17 closed 10 years ago

nicolasroger17 commented 10 years ago

I am using node-orm2 with express. I am trying to delete multiple lines in a database using remove. After all is deleted, i'd like to call a callback when i'm sure everything is done.

var idArray = [{id: 2}, {id: 5}]; I tried :

Person.find({or:idArray}).each().remove().save(function(){ res.writeHead(301, {Location: '/home'}); res.end(); }); but i'm told : cannot used remove on undefined.

So I have to do my method remove inside each

Person.find({or:idArray}).each(function(element){ element.remove(); }) .save(function(){ setTimeout(function(){ res.writeHead(301, {Location: '/home'}); res.end(); },2000); }); But the callback save is called before each remove is done.

So for the moment i'm using a timeout but I hope there is a better method.

Thank you for your answers.

nicolasroger17 commented 10 years ago

Person.find({or:idArray}).remove(function (err) { // handle the err here ! res.writeHead(301, {Location: '/home'}); res.end(); });