Here is my Controller
const getCountofCenters = async (req, res) => {
const result = Center.countDocuments();
await result.then(() => res.status(200).send(result))
.catch((err) => {
res.status(200).send(err)
});
}
This is the Api call
router.get("/count", CenterController.getCountofCenters);
This is the output which I get from the Postman test, empty array
{}
2
Answers
Try this:
model.countDocuments returns a promise that you need to resolve so you can get the value of count
so the correct implementation on this within an async function is
then sequentially you can return the response