1

Hope you are all staying safe.

So I am trying to get all the post of the friends of a current user but it does not work, I always receive a empty array, it is like the function send the array to the response before exercuting all of the loop.

So what I am doing is

  1. getting all the friends of the current user

  2. mapping over all his friends to get all the post of each friends

  3. Then I map over all his post and a push each post in an array

  4. Finally, I want the function to send the array that contains each post when all the loop is over but it does not work, I always get a empty array. I am king of new to the promise and async/await wolrd but I don't get it.

The function looks like this

exports.getFollowerPost = async (req, res) => {
    const id_user  = req.params.userUid;

    let friendsReference = db.collection('Friends').doc(id_user);

    let data = [];

    await  friendsReference.get().then((result) => {

        if (!result.exists) {
            console.log('No such document!');
            res.send('no docs exist')
          } 
          else {
              let friends  = result.data();

              let friendsOfUser = friends.follow;

               friendsOfUser.map((follow)  => {

                let postUser =  db.collection('Post').doc(follow);

                 postUser.get().then(item => {

                    const eachPost = item.data().post;

                    eachPost.map(eachPostUser => {
                        data.push(eachPostUser);
                        console.log("add post")
                    })
                }).catch(error => {
                    console.log(error)
                })
              })          
          }   
    })
    .catch(error =>  {
        res.status(400).send(error)
    })

    res.send(data) // always send a empty array ? why 
} 

2 Answers 2

3

Try this one.

exports.getFollowerPost = async (req, res) => {
  const id_user = req.params.userUid;

  let friendsReference = db.collection('Friends').doc(id_user);

  let data = [];

  try {
    const result = await friendsReference.get();

    if (!result.exists) {
      console.log('No such document!');
      res.send('no docs exist')
    }
    else {
      let friends = result.data();

      let friendsOfUser = friends.follow;

      friendsOfUser.map(async (follow) => {

        let postUser = db.collection('Post').doc(follow);

        const item = await postUser.get()

        const eachPost = item.data().post;

        eachPost.map(eachPostUser => {
          data.push(eachPostUser);
          console.log("add post")
        })


      })
    }
  } catch (error) {
    res.status(400).send(error)
  }

  res.send(data) // always send a empty array ? why 
} 
Sign up to request clarification or add additional context in comments.

4 Comments

You have to remove that bottom catch, but yeah besides that it looks good.
Very appreciate to the two of you for helping me . Thanks Julio for your code. I tried it but it did not work, I add the error, "await can only be use in async function", at first I was wondering where this error came from because I had clearly declare the async at the beginning of the function. I realize the problem was the map.(item => {}) that create a sync arrow function. I have replace it with a simple for loop and now it is working. Thanks
Now that I think about it you will want to stick to the then style promises for that loop. that or push all of the promises to an array and use Promise.all.
The way it is currently it only is operating on one async action at a time, which could considerably slow down the speed of this guy.
1

You are mixing the then and async/await methods for handling promises. Your first step would be sticking to just using await.

const result = await friendsReference.get()

So basically you are awaiting then, which is a handler of a promise, not a promise though so it doesn't wait for it to finish.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.