2017-05-31 55 views
0

Firebase db Sample data arrangment如何火力數據庫

訪問特定的子場如何訪問「名稱」標籤,並在設置每個獨特的火力鍵的值。請參考我的示例數據結構的附件截圖。

注意:這裏,「標籤」下的按鍵是通過推送功能生成的。

我已經在JS中嘗試了幾件事,但沒有工作。

ref.child("users").child("Tags").limitToFirst(1).on("child_added", function(snapshot) { 
console.log(snapshot.val()); }); 

回答

0

你可以用它child()方法訪問快照的子屬性:

ref.child("users").child(pushidofparent).child("Tags").on("child_added",function(snapshot) { 
    console.log(snapshot.child("name").val()) 
} 
+0

如果我在查詢中使用「child(」Tags「)」,它不會在控制檯日誌中顯示任何結果或錯誤。如果我刪除它,快照值顯示,但如果添加,然後空白。沒有錯誤。它不承認標籤是它的孩子,但爲什麼? – user1322692

+0

是的,我現在看到你在JSON中有一個額外的級別。你知道頂級推送ID嗎? –

+0

如果您在「用戶」之前詢問其數據庫的根目錄。 「用戶」確實有一些兄弟姐妹,但沒有父節點,只有root是父節點。 剛剛看到你的代碼,你是指「標籤的pushidofparent =鍵ID」? – user1322692

0

試試這個

ref.child("users").child("Tags").on("child_added",function(snapshot) { 
    snapshot.forEach(function(snap){ 
     console.log(snap.val()) 
    }); 
} 
+0

嘗試,但無輸出,在控制檯日誌中沒有錯誤。孩子(「標籤」)沒有顯示任何結果。如果我刪除它,然後可以記錄一些信息。爲什麼?任何想法... – user1322692