1

The 'GetUsers' function is running and it returns the correct values as expected. However, res.send(users) is running before the nested function has completed with the value so it is undefined. I have tried doing the res.send(users) from inside the nested function but it doesn't recognize the function. I have also tried using return users but can't get it to work. Any help would be appreciated!

app.get('/GetUsers', function (req, res){
    var url = 'mongodb://localhost:27017/users';
    var users = "";
    MongoClient.connect(url, function (err, db) {
        if (err) {
            console.log('Unable to connect to the mongoDB server. Error:', err);
        } else {
            console.log('Connection established to', url);
            // Get the documents collection
            var collection = db.collection('users');
            // Get some users
            collection.find().toArray(function (err, res) {
                if (err) {
                    console.log(err);
                } else {
                    console.log('Got Information from db.');
                }
                //Close connection
                db.close();
                console.log('db closed.');
                users = res;
                console.log(users);
            });
        }
    });
res.send(users);
});
Matrix
  • 21
  • 7
  • 1
    Possible duplicate of [How do I return the response from an asynchronous call?](http://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call) – Ben Fortune Apr 27 '16 at 08:20

3 Answers3

1

Just move res.send(users) inside your callback before/after you call console.log(users).

You reused the variable res in the toArray call, so please rename it there: ...toArray(function (err, res)... to ...toArray(function (err, result)... and also in users = res; to users = result;.

CFrei
  • 3,552
  • 1
  • 15
  • 29
  • I have tried that but I get a TypeError: res.send is not a function. – Matrix Apr 27 '16 at 08:39
  • Ups. Sorry, oversaw that you reused the variable `res`. Please rename `...toArray(function (err, res)...` to `...toArray(function (err, result)...` and also do that in `users = res;` to `users = result;`. – CFrei Apr 27 '16 at 08:47
  • Thanks for your response. Unfortunately I have tried this and still res.send(users) is running before the nested function completes. – Matrix Apr 27 '16 at 08:56
0

this is async call it will send the response and will not wait for the database connection function. write res.send in call back function
write your res.send function where you are closing your data base connection.

here is detail How do I return the response from an asynchronous call?

Community
  • 1
  • 1
Asad
  • 3,070
  • 7
  • 23
  • 61
  • Hi Asad, I have tried that but I get a TypeError: res.send is not a function. – Matrix Apr 27 '16 at 08:44
  • use different variable with funcntion ` collection.find().toArray(function (err, res) {` this is over riding the res fucntion @Matrix – Asad Apr 27 '16 at 08:49
  • use result or data instead of res in this function @Matrix – Asad Apr 27 '16 at 08:51
  • Thanks for your response. Unfortunately I have tried this and still res.send(users) is running before the nested function completes. – Matrix Apr 27 '16 at 08:56
0

Found the answer!!!

I needed to use res.send(users) in my callback after I had closed the database as pointed out by the users suggestions above. HOWEVER, that alone wasn't working as res.send(users) was erroring as it wasn't recognized as a function. I had to use res.send({'matched':users}) and then access my response objects on the client side like this: response.matched.objects etc.

Thanks for your help everyone!

Matrix
  • 21
  • 7