Quantcast
Channel: Active questions tagged react-native+typescript - Stack Overflow
Viewing all articles
Browse latest Browse all 6213

Firestore nested onSnapshot await/async

$
0
0

I'm having an issue with the onSnapshot method. It won't await for the second onsnapshot call, so the returned value is not correct. The users fetched in the second onsnapshot call, will be showed later in the console log when the value already has returned.

If you prefer an other structure, I am open to any solutions and suggestions!!:)Thank you all.

Console logs: https://imgur.com/a/vhm61nu.png

export const FirebaseGetStrikeLists2 = async (houseId: string) => {    let strikes: StrikeListDocument[] = [];    firestore()        .collection(FirebaseConstraints.HouseCollection)        .doc(houseId)        .collection(FirebaseConstraints.StrikeSubCollection)        .onSnapshot(async x => {            x.forEach(async x => {                const strikeListDocument = x.data() as StrikeListDocument                strikeListDocument.users = []                strikeListDocument.id = x.id;                x.ref.collection(FirebaseConstraints.UserCollection).onSnapshot(                    async x => {                        let strikeListUsers: StrikeListDocumentUser[] = [];                        x.forEach(async y => {                            let userDocument = y.data() as StrikeListDocumentUser;                            strikeListUsers.push(userDocument)                        })                        await Promise.all(strikeListUsers)                        console.log('users found: ', strikeListUsers.length)                        strikeListDocument.users = strikeListUsers                    })                console.log('users set in array: ', strikeListDocument.users.length)                strikes.push(strikeListDocument)            })        })    return strikes;}

My firestore structure looks like this:/houses/{HouseID}/strikelists/{StrikeListID}/users/{UserID}


Viewing all articles
Browse latest Browse all 6213

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>