Thanks to Frank over at Firebase for helping me with this code. I just had this one issue with pushing the document id under Friends collection. I am not sure what is the best way to push const friendId
and const accepted
to friendsList
array in the code below.
const db = admin.firestore();
const friendRef =
db.collection('users').doc(id).collection('friends');
friendRef.get().then((onSnapshot) => {
var promises = [];
onSnapshot.forEach((friend) => {
const personId = String(friend.data().person_id);
const friendId = String(friend.id);
const accepted = friend.data().accepted;
promises.push(db.collection('users').doc(personId).get());
});
Promise.all(promises).then((snapshots) => {
friendsList = [];
snapshots.forEach((result) => {
friendsList.push({
friendId: friendId,
accepted: accepted,
firstName: result.data().name.first,
lastName: result.data().name.last,
});
});
res.send(friendsList);
});
}).catch((e) => {
res.send({
'error': e
});
})
I tried a few things, but it didn't work. Any help would be appreciated.