0

I have this function that I want to return the value of a mongo query but I am getting undefined values. I am attempting to read the Email address of all of the users in the users collection of my test database.

This is my code:

var mongo = require('mongodb').MongoClient;
var connectionString = "mongodb://cannotdivulge.info.com:53778/testdb";
var users;
var db = mongo.connect(connectionString, function(err, db) {
    if(err)
        throw err;
    console.log("connected to database");
    users = db.collection('users');
});

exports.findAll = function(req, res) {
    var cursor = users.find();
    var result;
    cursor.each(function(err, doc) {
        if(err)
            throw err;
        result = doc.Email;
    });
    res.send(result);
};

result should be equal to the email address but it's undefined. The value of doc.Email is not being preserved. When I place res.send() inside cursor.each(), it will return a value.

Also, I will need to use the result variable in the function before actually returning it's value. That is why I need it's value to be preserved.

1 Answer 1

3

Your findall function is async. You'll need to return the results only after completing the list. In your code, the function was returning the value of result before the first callback for each had started.

exports.findAll = function(req, res) {
    var cursor = users.find();
    var result = [];
    cursor.each(function(err, doc) {
        if(err)
            throw err;
        if (doc === null) {
            // doc is null when the last document has been processed
            res.send(result);
            return;
        }
        // do something with each doc, like push Email into a results array
        result.push(doc.Email);
    });
};
Sign up to request clarification or add additional context in comments.

5 Comments

Correct. And if you're going to to grab a bunch of documents, place them in an array and send back to the client, you might consider using cursor.toArray(function(err, docs){}) which takes a callback for the whole result set. See cursor.toArray
@WiredPrairie But what if I want to use the result variable in another query before I return the value of the function? Like say I get the email and I want to check it against another collection? I need to use that variable again before returning it. That's why I need the value to not be undefined.
Wait till you get the result back, then do the next step? You're asking a new question it would seem?
@martingreber - yes, sure you can use toArray in some situations. It just automates a very simple loop using the same basic technique internally to an array. It presumes you've got enough RAM/memory to hold the array structure.
@WiredPrairie - I wasn't asking a new question, I guess I just didn't mention it initially but I edited my question. In any case, thanks for enlightening me with your answer. What I was trying to do is check that the users email was correct so that I could then use it (if the email is correct) to query the data collection where data is based on the users email.

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.