0

I'm trying to find all rows where post_id is the same as I'm searching for. But every time I do the result is empty, and I've double check all values and I should get some result.

This is the method I'm using:

db.collection('posts', function(err, collection) {
    collection.find({}, {}, {limit:100}).toArray(function(err, posts) {
        if (err) res.send({'error':1,'message':'error getting all posts'});
        console.log('Number of posts: ' + posts.length);
        posts.forEach(function(entry) {
            db.collection('love', function(err, collection) {
                collection.find({}, function(err, love) {
                    if (err) console.log(err);
                    if (!love) console.log("empty");
                    if (love) {
                        console.log(love);
                        entry.love = love;
                    }
                });
            });
        });
        res.send(JSON.stringify(posts));
    });
});

It always enter the third if like if I have a result but in the console I always get [] from the result. Any idea of that I'm doing wrong?

EDIT1: Code updated.

7
  • What happens if you try to console.log(love); with no JSON? Commented Jan 19, 2014 at 14:15
  • @Zorba it gives exactly the same result: [] Commented Jan 19, 2014 at 14:20
  • 1
    Hmm, the only other thing that I think could be wrong is that your find criteria is not working well... Maybe for debugging try changing to collection.find({} ..., so it returns all documents and you can check if the id really matches? Commented Jan 19, 2014 at 14:29
  • Could you post the code fragment that shows how entry is created? and the entry._id value is set? Commented Jan 19, 2014 at 14:42
  • @Zorba I tried and got some result. Commented Jan 19, 2014 at 15:06

1 Answer 1

1

The love in that callback is a cursor, not the array of docs. To make it an array, call toArray like you are in your first query; like this:

collection.find({}).toArray(function(err, love) {
    if (err) console.log(err);
    if (!love) console.log("empty");
    if (love) {
        console.log(love);
        entry.love = love;
    }
});

Also, your next problem will likely be that your res.send call won't include any of the love docs because it's happening before any of the love callbacks. You need to delay calling res.send until all callbacks have been received. You can do that by keeping a count of outstanding callbacks or using an async flow control module like async.

Sign up to request clarification or add additional context in comments.

4 Comments

thanks, I was wondering why res.send was executed before the rest. But I dont understand why that is executed before all the stuff that is before it in the function?
@just_user It's because the db.collection and collection.find calls are asynchronous; meaning the calls return immediately, and their callbacks aren't called until later.
ok, that makes sense. But I'm not sure how to do an async call for two queries. I mean, I need the posts query to be able to do the love query. How do I put that in an async task?
it's "weird", when you console.log(love), it displays [], but a conditional for if (love === []) doesn't catch it. console.log(typeof(love)) also displays object (when it is seemingly an array). the if (!love) conditional is not catching an empty result for me either. the only conditional that worked for me was if (love.length === 0).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.