Philippe Vincent
Philippe Vincent

Reputation: 91

async function return empty array, does not consider the await?

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 
} 

Upvotes: 1

Views: 2088

Answers (2)

Julio Ojeda
Julio Ojeda

Reputation: 817

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 
} 

Upvotes: 3

Matti Greff
Matti Greff

Reputation: 238

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.

Upvotes: 1

Related Questions