1

I want a function to return what is in the 'user' collection. But due to async nature function is returning undefined. How can i wait till mongodb assigns value to the variable

function a(){
    var m;
    MongoClient.connect(URL).then( db => {
        db.db('mydb').collection('user')
        .find({}).toArray().then(result => {
            m=result;
            // return m wont help here
        }).catch(log);
    }).catch(log);
    return m;
}
console.log(m);
Community
  • 1
  • 1

1 Answers1

0
function b(m) {
    console.log(m);
}

function a() {
    MongoClient.connect(URL).then(db => {
        db.db('mydb').collection('user')
            .find({}).toArray().then(result => {

                b(result);

            }).catch(log);

    }).catch(log);

}
Alex Baban
  • 11,312
  • 4
  • 30
  • 44
  • Great! I just missed that solution. You helped me thank you so much :) –  Apr 29 '18 at 16:59