Mongoose: Model.find().exec(function (err, data) { do something })

Created on 22 Sep 2013  路  4Comments  路  Source: Automattic/mongoose

Take a look at "myObject" and "pers"
Why we can not print out the value of "myObject" when it is out side "exec" function. The value of myObject when it out side is "undefined"

image

Thank so much!

Most helpful comment

You can user waterfall/async method for solve this problem.

All 4 comments

This is a problem in your code logic and callbacks. For all of the console.log calls other than the one directly after myObject is set, they will get called _before_ anything is actually set.

So what should I do if I want do something on return data (likely combine with other data) before responding it to client. Do I need some delay time before get it?

generally, you do what you need to within the callback function. You could theoretically use a time delay, but that is not a good way to do it and unreliable.

You can user waterfall/async method for solve this problem.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

Soviut picture Soviut  路  3Comments

simonxca picture simonxca  路  3Comments

Igorpollo picture Igorpollo  路  3Comments

jeneser picture jeneser  路  3Comments

lukasz-zak picture lukasz-zak  路  3Comments