-1

How can I get all the documents from mongodb using nodejs SDK ? I tried following approach to get all the documents but could't get any however insertions is working fine.

    // Connection URL
    var url = config.mongodbConnectionString;
    var db: any;

    // Use connect method to connect to the Server
    MongoClient.connect(url, function (err: any, database: any) {
        assert.equal(null, err);
        console.log("Connected correctly to server");
        db = database;
    });    

export class MongodbProvider implements IDbProvider {    

    public getMenus(): any {
    var menus: any = [];
    try {

        db.open(function (err, db) {
            var cursor = db.collection('menus').find(function (err, cursor) {
                cursor.each(function (err, doc) {
                    console.log(doc);
                     menus.push(doc);
                });
            });
        });
    }
    catch (err) {
        console.log(err);
    }
    return menus;
}
}

Can you please help me in this?

Deepak
  • 1,510
  • 1
  • 14
  • 27
  • 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) – ponury-kostek Apr 18 '17 at 09:49

1 Answers1

0

First parameter of find is a filter. To fetch everything, it should be an empty document. Something like:

var cursor = db.collection('menus').find({}, function (err, cursor) {
Alex Blex
  • 34,704
  • 7
  • 48
  • 75