簡體   English   中英

如何從另一個文檔angularId的Id獲取文檔

[英]How to get a document from Id gotten from another document angularFirestore

我在嘗試從另一個文檔中的一個文檔中獲取信息時遇到了一個問題,我需要一個文檔,因為它具有Id屬性。

在以下img上,我可以訪問我的Diets集合並獲取我的特定文檔,但是我無法“回來”並獲取我的信息ID:

在此處輸入圖片說明

然后,我的用戶結構如下: 在此處輸入圖片說明

一旦暴露了問題,我將向您顯示無法繼續執行的代碼:

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;
     }
  }

});

任何幫助將不勝感激。

我認為您正在尋找這樣的東西:

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);
        });
      });
    }
  }    
});

因此,一旦確定存在注釋,此代碼便會遍歷注釋,然后為每個注釋獲取用戶。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM