Skip to content Skip to sidebar Skip to footer

Firestore - Getting Data From A Reference Like 'collectionname/{id}' And Iterating Over The Resulting List

I'm trying to get all data from 'anunturi_postate' reference list. Variable 'post_data' pushed to the list looks ok in the loop, but outside the loop, the list it's empty. I added

Solution 1:

async/await does not work inside a forEach loop in the way you expect. The loop does not return a promise that you can await, and will not finish before moving on to the following code. Use a for loop instead. Also, you can use each reference as if it was a DocumentReference object.

    let user_posts = []
    const array = query.data().anunturi_postate
    for (let ref of array) {
        let post = await ref.get()
        let post_data = await post.data() 
        console.log(post_data) // ok
        user_posts.push(post_data)
    })

    console.log("user_posts:", user_posts)

Post a Comment for "Firestore - Getting Data From A Reference Like 'collectionname/{id}' And Iterating Over The Resulting List"