简体   繁体   中英

How to get a document from Id gotten from another document angularFirestore

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:

在此处输入图片说明

Then, my structure of users is the following: 在此处输入图片说明

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM