I have 2 mongoose schemas and i am getting data from it , but before the data is resolved from the schemas , the response is being sent.
//I have tried some callback hacks but it doesnt work.
router.get("/api/getPosts", (req, res) => {
let array = [];
post.find({}).then(posts => {
posts.forEach(post => {
post.likes.forEach(like => {
dummy.find({ _id: like.likeUserId }).then(user => {
array.push(user);
});
});
});
});
res.send({data: array})
});
I have atleast 2000 post coming from the database. And before the users data who have liked the post is pushed in array, it sends the response and the data comes empty.