0

How do I return result" when collection.each is finish? I tried using map but collection isn't an array.

function(arg) {
    var result = [];
    db = MongoInternals.defaultRemoteCollectionDriver().mongo.db;
    collections = db.listCollections();

    collections.each(function(n, collection) {
        if (collection) {
            result.push(collection.name)
        }
    });
    return result;
    }
Mag Ron
  • 51
  • 5
  • I read those, couldn't comprehend how to use promise in this scenario or use async/await. – Mag Ron Jan 09 '20 at 14:01
  • It doesn't have to be a promise not the async/await. A callback would do. – Wiktor Zychla Jan 09 '20 at 14:03
  • The duplicate has 101 answers. If you can't comprehend any of them then there isn't much we can really add. – Quentin Jan 09 '20 at 14:04
  • The result is return to the nested function ? Just taking it out the foreach loop ? ``` function(arg) { var result = []; db = MongoInternals.defaultRemoteCollectionDriver().mongo.db; collections = db.listCollections(); collections.each(function(n, collection) { if (collection) { result.push(collection.name) } }); return result; } ``` – bLaXjack Jan 09 '20 at 14:05
  • I am actually generating a function using Meteor.methods({ [updateText.name]: function (args) { updateText.validate.call(this, args); updateText.run.call(this, args); }). then use call with Meteor.call('method1', aaa, function (error, result) { console.log(result); }); How would I generate a callback in this scenario. – Mag Ron Jan 09 '20 at 14:07
  • @bLaXjack i edit the code. i tried that. result would be undefined. – Mag Ron Jan 09 '20 at 14:10
  • 1
    @MagRon i think this might help https://stackoverflow.com/questions/30470415/listing-all-collections-in-a-mongo-database-within-a-nodejs-script – bLaXjack Jan 09 '20 at 14:14

0 Answers0