Monday, 10 April 2017

How do I send multiple queries from one endpoint with Express?

I am trying to query my database several times and construct an object which stores every response from my database in a field. Here is my code:

router.post('/search', (req, res) => {
    var collection = db.get().collection('styles')
    var data = [];

    collection.distinct('make.name', (err, docs) => {
      data.push({'make': docs });
    });

    collection.distinct('model', (function (err, docs) {
        data.push({'model': docs });
    }))

    res.send(data);
});

Since NodeJS/Express is asynchronous, this isn't working as I would like. How can I reconstruct this endpoint to make several database calls (from the same collection) and return an object containing it?



via Moshe

No comments:

Post a Comment