Farzher
Farzher

Reputation: 14563

Node MongoDB execution order

I'm using collection.insert then in the callback collection.findAndModify

About 10% of the time collection.findAndModify fails to find the document I just inserted, even though it's being executed after the insert callback. Why is this possible? How do I deal with that?

enter image description here

I insert it, then try to modify it, but it's not there, then try to modify it again and it's there.

Upvotes: 0

Views: 212

Answers (1)

ma08
ma08

Reputation: 3744

You should give the second command in the callback as the insert is asynchronous. If you are using the mongodb native driver for node,

collection.insert(document, function(err, records){
     //call collection.findAndModify here

});

Check the docs

Upvotes: 1

Related Questions