On Sat, Dec 8, 2012 at 10:02 AM, Jimmy Haver <[email protected]> wrote:

> Here is the none working format, this is in a function that should return
> results when db is finished.
>

Since the code is async, you can't actually *return* when the results have
been collected. You'll need to update your code such that it calls a
callback when you have the results. See below.


>
> var results = [];
> dependencies.forEach(function(dependency) {
>   db.collection('parts_relations').find(query).toArray(function(err,
> relations) {
>     relations.forEach(function(relation) {
>       results.push(relation);
>     });
>   });
> });
>
> I have tried two things without success:
> (this returns before finishing)
> var results = [];
> var counter = 0;
> dependencies.forEach(function(dependency) {
>   counter++;
>   db.collection('parts_relations').find(query).toArray(function(err,
> relations) {
>     relations.forEach(function(relation) {
>       results.push(relation);
>     });
>     if (counter >= dependencies.length) {
>       return results;
>

Here, you are returning the results from the callback passed to toArray(),
not the function you're thinking you're returning from, I think. However,
if you modify your enclosing function to take in a callback, as mentioned
above, then you can call that callback here, instead of trying to return.

--
Martin Cooper



>     }
>   });
> });
>
> I also tried wrapping db.collection in a function and passing it to
> async.parallel without luck. Any suggestions?
>
> --
> Job Board: http://jobs.nodejs.org/
> Posting guidelines:
> https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
> You received this message because you are subscribed to the Google
> Groups "nodejs" group.
> To post to this group, send email to [email protected]
> To unsubscribe from this group, send email to
> [email protected]
> For more options, visit this group at
> http://groups.google.com/group/nodejs?hl=en?hl=en
>

-- 
Job Board: http://jobs.nodejs.org/
Posting guidelines: 
https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
You received this message because you are subscribed to the Google
Groups "nodejs" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/nodejs?hl=en?hl=en

Reply via email to