0

I have three columns in mongoDB. I want to retrieve data from the DB using node.js. I used the below code.

var myCursor = Users.find({USER_MOBILE_NUMBER : Auth.USER_MOBILE_NUMBER} , function(err , success){
        console.log('Response success '+success);
        console.log('Response error '+err);
    });

    myCursor.forEach(function(race) {
    console.log(race);    
    });

But when I run the code it returns null in the log for 'race'. I am new to node.js and I also searched a lot to find a solution, but I failed. Please help me where I went wrong in the code.

1 Answer 1

2

It's an asynchronous function, you need to do your logic from within the callback as the find method doesn't actually return anything.

Users.find({USER_MOBILE_NUMBER : Auth.USER_MOBILE_NUMBER} , function(err , myCursor){
    myCursor.forEach(function(race) {
        console.log(race);    
    });
});
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you for the reply. It works fine. I would like to know more about the asynchronous functions and node.js. So pls give me some website references with examples.

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.