I have an array of user Ids as 'members' passed into the getMemberAvatars() function. I take each id, look it up in the users collection, and put their photoURL in an array called memberAvatars. Once I have that array of URLS, I can pass JSX back using map. However, the memberArray of URLS is populated in a callback - so my JSX renders before it's back.
I need to wait for the result. Perhaps async/await can help? Other ideas?
getMemberAvatars(members) {
let memberAvatars = [];
members.forEach(userId => {
db.collection("users")
.doc(userId)
.get()
.then(doc => {
memberAvatars.push(doc.data().photoURL);
});
});
return memberAvatars.map((url, index) => <Avatar src={url} />);
}
Based on Doug's comments below, tried this to no avail....
getPhotos(members) {
let urls = [];
for (let userId in members) {
db.collection("users")
.doc(userId)
.get()
.then(doc => {
urls.push(doc.data().photoURL);
});
}
return urls;
}
async getMemberAvatars(members) {
let results = [];
let urls = await this.getPhotos(members);
await Promise.all(
urls.map(async url => {
results = await (<Avatar src={url} />);
})
);
return results;
}