Home > Back-end >  mongoose find 2 collection on one get request
mongoose find 2 collection on one get request

Time:10-11

i have a simple question that i want to understand better request

i have a get request that need to give me this 2 collection one for the product and the other is for the products the code that you see now is worked like i want it to be but i want to know if this is the correct approch for this mongoose find methos

what that i have is take users and the products to an array and send it back to the client side

router.get("/", async (req, res) => {
  await UserModel.find({}, (err, users) => {
    ProductModel.find({}, (err, products) => {
      const temp = [users, products];
      res.json(temp);
    });
  }).clone();
});

CodePudding user response:

Don't mix async / await and callbacks

router.get("/", async (req, res) => {
  const temp= await Promise.all([
    UserModel.find({})
    ProductModel.find({})
  ])
  res.json(temp);
});
  • Related