0

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

Carlos
  • 458
  • 12
  • 23
  • What is `.findOneById()`? Are you actually using mongoose? And do you mean `.findById()` then, or did you define your own static method for some reason? This also appears to be a "join", and therefore `.populate()` or `$lookup` are really the direction you should be going in. The problem here is "promises in a loop", but there are other ways to solve this even without the loop if you could be a bit more clear on the source and expected result. – Neil Lunn Nov 16 '17 at 05:07
  • yes `.findOneById()` and `.findById()` are the same. I just modified – Carlos Nov 16 '17 at 06:02
  • Like I said. You basically are attempting a "join". Look at [`$lookup`](https://docs.mongodb.com/manual/reference/operator/aggregation/lookup/) and make an attempt to use it. This avoids the problem of looping async calls because it's only **"one"** call to the database. If you have problems understanding how to write with `$lookup` then there are existing questions and examples, and you can always [Ask a new Question](https://stackoverflow.com/questions/ask) related to any problems you encounter that you cannot see a solution to. But try it first. – Neil Lunn Nov 16 '17 at 06:06

0 Answers0