Monday, 3 April 2017

res.send after two forEach have finished executing

const collect = [];
req.body.product.forEach(function(entry) {
    mongoClient.connect(databaseServerUrl, function(err, db) {
        let testCollection = db.collection('Tests');
        testCollection.find({Product: entry}).toArray((err, docs) => {
            let waiting = docs.length;
            docs.forEach(function (doc) {
                collect.push(doc);
                finish();
            });
            function finish() {
                waiting--;
                if (waiting === 0) {
                    res.send(collect);
                }
            }
        });
        db.close();
    });
});

this is only getting back the first set. If I have two nodes in my array of req.body.product for example. I am only getting back the first set. But I need to get back everything not just from one Collection.



via mkteagle

No comments:

Post a Comment