return multiple queries from backend return multiple queries from backend mongoose mongoose

return multiple queries from backend


You can't send multiple responses using HTTP. HTTP requests to responses are 1:1 so when a request is sent you will ALWAYS expect only one response. Anything else and it would get quickly messy. So, how to send multiple sets of data?

You could do something like this:

router.get("/test/:id", (req, res) => {  let result = {}  example.find({test: req.params.id})    .then(data => {        result['partOne'] = data.map(moreData => moreData.serialize())        return differentExample.find({something: req.params.id})    }).then(data => {        result['partTwo'] = data.map(moreData => moreData.serialize())        res.json(result)    })}

Note: I haven't tested any of this code. But in essence, do both requests, and once you have the result of both requests you can return the result. If the requests don't depend on each other, you could use something like this using Promise.all as you mentioned:

Promise.all(    example.find({test: req.params.id},     differentExample.find({something: req.params.id})).then(result => {    res.json(result);})