0

I want to track the number of documents I have within a collection in a node.js server using mongodb driver. I can insert, delete and update propperly but when I try to count, it works until I try to store that value, moment in which it returns nothing.

Here is my code:

var db_collection = db.collection('collection');

var countCollections = function () {

    var response_count_collections = null;
    db_mensajes.count(function(err,number_of_collections){
        if (err){
            console.log(err);
        } else {
            response_of_collections = number_of_collections;
            console.log('Number of collections: '+number_of_collections);
        }
    });
    return response_count_collections;
};

It logs the number_of_collections correctly but it doesn't return me the right value. In my example it returns null (how I defined my var response_count_collections;) and if I try to return number_of_collections; within the db_collections.count()'s callback like this:

var db_collection = db.collection('collection');

var countCollections = function () {

    var response_count_collections = null;
    db_mensajes.count(function(err,number_of_collections){
        if (err){
            console.log(err);
        } else {
            console.log('Number of collections: '+number_of_collections);
            return number_of_collections;
        }
    });
};

It returns me "undefined". Is there any way to achieve what I want?

Stennie
  • 63,885
  • 14
  • 149
  • 175
Ruben Marrero
  • 1,392
  • 1
  • 10
  • 23
  • 1
    Your problem is that you're combining synchronous and asynchronous programming inappropriately. By the time your function reaches the return point `dm.messages.count` may have not even executed. – Edwin Dalorzo Aug 11 '14 at 04:29
  • but the result is the same if I try to return the counter like this: (edited with the code in the OP). – Ruben Marrero Aug 11 '14 at 04:32
  • 1
    You don't use `return` in asynchronous programming. You should use callbacks instead. – Edwin Dalorzo Aug 11 '14 at 04:52
  • possible duplicate of [How to return the response from an Ajax call?](http://stackoverflow.com/questions/14220321/how-to-return-the-response-from-an-ajax-call) – Leonid Beschastny Aug 11 '14 at 09:19

1 Answers1

1

Its because it returns the variable before the function is completely executed.

If you want it to be asynchronous then you will have to learn to control the flow of the program using some node modules like async.

Update:

Have a look at this question, it shows how to return value from an async function using callback correctly.

Community
  • 1
  • 1
SuperAzeem
  • 157
  • 1
  • 11
  • But even if I try to return the value within the callback it logs the value and returns undefined. – Ruben Marrero Aug 11 '14 at 04:35
  • 1
    The way you are returning the value from the callback isn't correct. Have a look at this question, it shows how to return the value from a async function using callback. http://stackoverflow.com/questions/18950984/return-value-from-asynchronous-function-in-nodejs – SuperAzeem Aug 11 '14 at 04:58