1

I amb having a problem trying to get information from one document inside the other document, the one I need because it has the property Id.

On the following img, I can access to my diets collection and get my specific document, but then I am not able to 'come back' and get my info ID:

enter image description here

Then, my structure of users is the following: enter image description here

Once I exposed my problem, I'll show you the code where I am not able to continue:

commentsDietC: AngularFirestoreDocument<any>;
commentsDiet : Observable<any>;

 this.commentsDietC = db.doc<any>('diets/'+this.diet.idDiet);
 this.commentsDiet = this.commentsDietC.snapshotChanges()
 .map(actions => {
  if(actions.payload.data()){
    if(actions.payload.data().hasOwnProperty('commentsDiet')){

      /*HERE is where I should try to get the displayName and PhotoUrl from 
      the other document*/
      return actions.payload.data().commentsDiet;
     }
  }

});

Any help would be really appreciated it.

Joan
  • 233
  • 1
  • 5
  • 17
  • By reading your comments you are already doing what i can think of, maintain an array and push the user object in the loop. The problem with your database design is every time a new comment added you have to rebuild entire comments array. The solution is to maintain `commentsDiet` as sub collection, and each comment will be a document. – Hareesh May 21 '18 at 05:05

1 Answers1

0

I think you're looking for something like this:

this.commentsDiet = this.commentsDietC.snapshotChanges()
 .map(actions => {
  var data = actions.payload.data();
  if(data){
    if(data.hasOwnProperty('commentsDiet')){
      data.commentsDiet.forEach(comment => {
        var userDocRef = firebase.firestore().doc('users/'+comment.idUser);
        userDocRef.get().then(userDoc => {
          console.log(userDoc.data().displayName);
        });
      });
    }
  }    
});

So once you've determined that there are comments, this code loops over the comments, and then gets the user for each comment.

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
  • Hi @Frank van Puffelen, I've copied your code, but on the userDocRef.get() appears an error telling that the method get does not exist on type AngularFirestoreDocument – Joan May 14 '18 at 14:40
  • Ah, I didn't realize it was an `AngularFirestoreDocument`. I've updated to use the regular Firestore JavaScript SDK. You'll also want to look at other questions where AngularFire users joined data from two collections, such as https://stackoverflow.com/questions/47577689/angularfire-combine-two-firestore-collections-with-the-same-pushid-items and https://stackoverflow.com/questions/41755579/how-to-get-do-a-join-in-angularfire2-database – Frank van Puffelen May 14 '18 at 14:43
  • Thank you so much for your help Frank, I'll check these links! – Joan May 14 '18 at 14:45
  • Hello @Frank, after checked your links, I've ended up thinking that my problem is different. The way I solved is a bit dirty, I've created an array, and once I am on the User Document, create a new object and pushing it, and then display it on the template. I guess is not the best solution, but is the only way I could figured out. Thanks for your time. – Joan May 15 '18 at 21:22