I want to access a subcollection called 'followers' from a given id under the 'mobile_user', this contains the id's of the followers. I want to loop through each of the id's in the followers subcollection and query data from the 'mobile_user' collection . Notice that the id in the 'followers' subollection is the id of another user under 'mobile_user' which contains the document data i want.
I've tried messing around with promises with no luck, i'm able to do the foreach loop and just query the name as a test but the array gets populated correctly but the populated array with the names of the N users are never returned.
I need some assistance with my code, promises are driving me nuts I can't get the hang around them.
const getFollowers = (data, context) => {
return new Promise((resolve, reject) => {
let id = data.id
const mobileUserRef = db.collection('mobile_user')
return mobileUserRef.doc(id).collection('followers')
.get()
.then(function (doc) {
var result = []
doc.forEach(function (follower) {
mobileUserRef.doc(follower.id).get()
.then(function (followerdoc) {
result.push({
name: followerdoc.data().name,
})
console.log(result)
})
})
return Promise.all(result);
})
});
}
The expected result is an array with the data of every id under the followers sub, like this:
In this example only 2 user id's are present
[ { name: 'Luis' }, { name: 'Marb Rocha' } ]