I have an area of my code where users of a group are stored in an array. In some cases, I want to gather additional data on the users.
Here is what I tried so far:
async getMembersData(groupUid) { const members = await this.getMembers(groupUid); var membersData = {}; Object.keys(members).map((key, i) => { const member = members[key]; firebase.database().ref(`users/${member.uid}`).once('value', snap => { membersData[member.uid] = { 'uid': member.uid, 'username': snap.child('username').val(), 'imageUrl': snap.child('imageUrl').val(), 'type': member.type, }; }); }); return membersData; } I have this code working similarly by setting state and using the firebase .on() function, but I need it to work with .once() and returning the object.
on(), but it'll actually be returning an empty object on the first call togetMembersData. You'll need to deal with the fact that the code is asynchronous, for example by using the approach in Renaud's answer.