I know this has to do with async but I can't figure it out.
This returns an empty object. The functions.logger.log(groupName) is logging a value so I know there's data there.
exports.getMinistryMembers = functions.https.onCall((req) => {
// calls
const getMinistry = admin.database().ref(`organization/${req.orgId}`);
const getMinistryForUser = admin.database().ref(`organization/${req.orgId}/attendanceOptionsMinistries`)
const ministryMembers = admin.database().ref(`organization/${req.orgId}/inMinistries`);
const ministryGroup = admin.database().ref(`organization/${req.orgId}/ministryGroups`);
return getMinistryForUser
.child(req.uid)
.once('value')
.then(snapshot => {
if (snapshot.val()) {
const ministryUids = snapshot.val();
return Object.keys(ministryUids).map((ministryItem, i) => {
// get name of group
return ministryGroup
.child(ministryItem)
.once('value')
.then(groupSnap => {
const groupName = groupSnap.val();
functions.logger.log(groupName);
return groupName;
})
});
}
})
});
I would expect the value to return since it's in a then call but I'm not sure what I'm missing. As a side note this works but not the second then.
exports.getMinistryMembers = functions.https.onCall((req) => {
// calls
const getMinistry = admin.database().ref(organization/${req.orgId});
const getMinistryForUser = admin.database().ref(organization/${req.orgId}/attendanceOptionsMinistries)
const ministryMembers = admin.database().ref(organization/${req.orgId}/inMinistries);
const ministryGroup = admin.database().ref(organization/${req.orgId}/ministryGroups);
return getMinistryForUser
.child(req.uid)
.once('value')
.then(snapshot => {
if (snapshot.val()) {
const ministryUids = snapshot.val();
return Object.keys(ministryUids).map((ministryItem, i) => {
return ministryItem;
});
}
})
});