I am trying to display data from tow collection. I have connected to child(user) collection inside parent collection and fetching some basic details of user from user(child) collection and displaying along with parent collection data. The problem is sometimes data from user collection doesn't display. If I refresh the page again then it displays. sometimes displays, sometimes doesn't display. and if I refresh it displays. Checked in postman as well by calling API but same problem. sometimes response not displaying from user collection. But parent collection data displays every time. Can you please tell me how can I fix this. Please give me solution for this.
const viewAllReviews = (req, res, next) => {
ParentTable.findOneById(req.params.id)
.then((resp) => {
const r = resp;
if (resp.user_ratings.length > 0) {
for (let i = 0; i < resp.user_ratings.length; i += 1) {
ChildTable.findOneById(resp.user_ratings[i].userId)
.then((users) => {
r.user_ratings[i] = {
ratings: resp.user_ratings[i].ratings,
comments: resp.user_ratings[i].comments,
comment_added: resp.user_ratings[i].comment_added,
comment_updated: resp.user_ratings[i].comment_updated,
commentId: resp.user_ratings[i].commentId,
userId: resp.user_ratings[i].userId,
// data from user collection
username: users.username,
displayname: users.displayname,
profilepicture: users.profilepicture
};
if (i === resp.user_ratings.length - 1) {
res.result(resp.user_ratings, 200, next);
}
});
}
} else {
res.result(r.user_ratings, 200, next);
}
})
.catch(e => next(e));
};
the result from user collection doesn't display sometimes. If I refresh again then it displays