Im trying to get some Ids from a collection, and then trying to use each of these Ids to get more data from another collection. Here is what I got done so far:
Basically, I have blog app in which multiple users will be able to post an article. And said article has categories. The thing is, when a user selects a category to filter, it will bring me all USERS that have an article in said category (instead of bringing all articles of the selected category).
So, in Firestore I have:
categories/{categoryId}/users/{userId}
Although the {userId} has more info inside it (like the articles ids in this {categoryId}), I'm just interested in the userId itself for now.
What I want to do is to use the recovered userIds from the above path, and use it to get the user data from the following path:
users/{userId}
And here's where I'm stuck and not sure how to proceed next.. Should I store the ids in a array, and then make requests to Firestore using a forEach loop in said array? Should I duplicate the user data on categories/{categoryId}/users/{userId}? Perhaps change my Firestore data structure? Or perhaps theres a different way to do it in Angular?
this.afs.collection<Category>('categories').doc(category.name).collection<User>('users')
.snapshotChanges().map(docChangeActionArray => {
return docChangeActionArray.map(value2 => {
return value2.payload.doc.id;
})
}).subscribe(ids => {
//WHAT TO DO HERE?
})
I apologise for such a simple question. I'm still getting the hang of Angular and Firestore, but appreciate any help!
Thanks.
UPDATE
I did the following:
.subscribe(ids => {
ids.map(id => {
this.afs.collection('users').doc<User>(id).valueChanges().map(user => {
return user;
}).subscribe(user => {
console.log(user); //Got it!
})
})
})