I want to retrieve information of all the users who are friends of a particular user. To return a single json array I am pushing data first to an array and then returning the data but looks like data is returning empty due to asynchronous behavior of firestore queries.
Can anyone please help how to return data after all values get pushed to it.
export async function getUserFriendsDetails(userId) {
var data = {
results: []
}
getUser(userId).then((snapshot) => {
snapshot.data().friends.forEach(friend => {
getUserDetails(friend).then((result) => {
data.results.push(result.data()) // data is getting pushed here
})
})
})
return data; // data is empty array here
}
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…