1

I am getting data from firestore and then pushing data to array which returns an empty array. But items are exist

let viewData = []


 firestore.collection('views').where('viewid', '==', props.user.uid).orderBy('viewat', 'desc').limit(6).get().then(views => {
   if (views.empty === false) {
      views.forEach(item => {
         firestore.collection('userdata').doc(item.data().myid).get().then( user=> {
            viewData.push(user.data())
        })
        // +++ Working fine here +++
        // viewData.push(item.data())
    })
  }
  console.log(viewData)
})

Above code prints an empty array in the console. But elements are exist inside the array. When we try to push item.data() is working fine. At the same time Array.push is not working for the user.data(). I am not sure why Array.push behaves in this manner.

Mahesh
  • 119
  • 2
  • 6

0 Answers0