簡體   English   中英

如何將Firebase中特定ref的所有子級放入對象?

[英]How do you get all the children of specific ref in firebase into an object?

這是我的Firebase結構。

在此處輸入圖片說明

我想像這樣把它全部拿回來。

 {
"users": {
    "EFo7BemCuWVFyGYwk0CiTHR5fDB2": {
        "profile": {
            "-LMUD--2APhiSLkEtVbM": {
                "profileName": "bob",
                "profilePic": "https://scontent-iad3-1.xx.fbcdn.net/v/t1.0-1/c27.0.160.160/p160x160/205110_1018814483408_6876_n.jpg?_nc_cat=0&oh=2e40387a728c2f8c8d04b3299601be7e&oe=5C18FC6D",
                "score": 73,
                "teamName": "lzs"
            }
        },
        "team": {
            "-LPpWX5o8kmfCKUZBoYy": {
                "competitorKey": "-LNb6AEdyBSmeooeDhT0"
            },
            "-LPpWX_SMWHWsKauutVC": {
                "competitorKey": "-LNb42VDA4ZjW2EeVLLd"
            }
        }
    },
    "FIXBObMxXoOzFE7hsb4wHl8esfe2": {
        "profile": {
            "-LPqBizyqR-jEAsawYO7": {
                "profileName": "steve",
                "profilePic": "http://www.cricearena.com/images/rough_riders_new_medium_logo-u2182.png",
                "score": 43,
                "teamName": "twitterRangers4"
            }
        },
        "team": {
            "-LPqX9ncuK09WvnT3SIk": {
                "competitorKey": "-LNb6AEdyBSmeooeDhT0"
            }
        }
    }
  }
 }

我得到的只是用戶的密鑰。

我的redux動作是

export function loadLeague() {
 return dispatch => {
leagueFireDB.path = `users/`;
leagueFireDB.subscribe(dispatch);
 };
}

我的高級fb db函數。

    subscribe(emit) {
let ref = firebaseDb.ref(this._path);
let initialized = false;
let list = [];

ref.once('value', () => {
  initialized = true;
  console.log("ONCE::",this._actions)
  emit(this._actions.onLoad(list));
});

ref.on('child_added', snapshot => {
  if (initialized) {
    emit(this._actions.onAdd(this.unwrapSnapshot(snapshot)));
  }
  else {
    list.push(this.unwrapSnapshot(snapshot));
  }
});

ref.on('child_changed', snapshot => {
  emit(this._actions.onChange(this.unwrapSnapshot(snapshot)));
});

ref.on('child_removed', snapshot => {
  console.log("REMOVE2::",snapshot)
  emit(this._actions.onRemove(this.unwrapSnapshot(snapshot)));
});

this._unsubscribe = () => ref.off();
}

因此很明顯,它命中了數據庫中的“用戶”節點,並在那里獲取了這些密鑰。 它不會遍歷並返回所有屬於ref的孩子(或者我猜是大孩子,等等)。 我想在數據庫中獲取Users引用的所有子級,但是我一直在尋找,沒有找到任何能做到這一點的東西。 我反應redux火力基地。 而已。 沒有API /中間件。

因此,這給了我完整的目標,包括我一直在尋找的所有子孫。

 subscribeChild(emit) {
   let ref = firebaseDb.ref(this._path);
   ref.once('value', snapshot => {
   emit(this._actions.onLoad(this.unwrapSnapshot(snapshot)));
   });
 }

這是我的包裝。

 unwrapSnapshot(snapshot) {
   let attrs = snapshot.val();
   attrs.key = snapshot.key;
   return new this._modelClass(attrs);
 }

我的加載操作請求正在調用ref.once高級別的Firebase數據庫調用,但我沒有在此處展開​​快照。 因此,我僅針對此加載請求創建了另一個名為subscribeChild的訂閱發射器,並將unwrapSnapShot放入返回的發射器中。 確實,整個對象都包含所有子引用和鍵值數據。 現在,我必須弄清楚如何處理數據對象。 :)

我只是在遍歷此Firebase RTDB的方式,所以如果有人有任何評論,或者如果我確實做錯了,請發表評論,我將相應地更新或接受更好的答案。

暫無
暫無

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

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