我有這個資料庫:

如果 members_uid 與用戶匹配,則此處的主要目標是顯示組名。我可以使用 user_uidauth.currentUser
我目前只是想列印members/members,所以我可以使用 if 陳述句并在我的 return 陳述句中顯示結果(使用React)
我試過的:
const db = getDatabase();
const dataRef = ref(db, '/groups');
onValue(dataRef, (snapshot) => {
const childKey = snapshot.key;
const data = snapshot.val()
const key = Object.keys(data);
console.log(data)
console.log(key)
console.log(childKey)
})
childKey= 組
key= 回傳所有 firebase 生成的密鑰(例如 -N02Qrg...)
data= 回傳所有內容

我如何獲得組/成員/成員uid?
uj5u.com熱心網友回復:
由于您正在閱讀groups,因此snapshot您獲得的 包含該路徑下的所有資料。要瀏覽快照,您有兩個主要功能:
snapshot.child("name")允許您獲取您知道其名稱的子節點的快照。snapshot.forEach()允許您回圈遍歷所有子快照,通常是在您不知道它們的名稱時。
通過結合這兩者,您可以導航任何結構。對于你的 JSON,我會做這樣的事情:
const db = getDatabase();
const dataRef = ref(db, '/groups');
onValue(dataRef, (snapshot) => {
snapshot.forEach((groupSnapshot) => {
console.log(groupSnapshot.key); // "-N02...R1r", "-N02...1T8"
console.log(groupSnapshot.child("g_id").val()); // "jystl", "nijfx"
snapshot.child("members").forEach((memberSnapshot) => {
... // each of the child snapshots of the `members` nodes
});
})
})
請注意,在單個父節點下嵌套多種型別的資料是 Firebase 上常見的反模式,如有關structuring data的檔案中所述,特別是有關避免構建嵌套和保持資料平坦的部分。
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/459692.html
