2016-11-02 31 views
1

我對firebase很陌生。當我遇到一些非常酷的事件時,我只是嘗試了一些例子。child_changed事件firebase

我已經保存在數據庫中火力一些數據是:

driver 
     -KV_Cj7sL6sg6K9E5ifh 
     status: "Incomplete" 

的一件事情困擾我的是,當一個孩子被更新child_changed事件被觸發。當我訪問它的數據時,我沒有看到它的關鍵。

我在驅動程序表中創建了一個節點(我認爲一行在firebase中稱爲節點不知道)。當我改變它的狀態來完成它觸發的事件時。我收到已更新的數據包。事情是我想獲得Id(KV_Cj7sL6sg6K9E5ifh)。但問題是它只是給我發送狀態:完整。

這是我爲child_changed編寫的代碼:

usersRef.on("child_changed", function(data) { 
    var player = data.val(); 
    console.log(player); 
    console.log("The new status is: "+ player.status); 
}); 

請幫我,我怎麼會收到標識了。由於

+0

交叉後:https://github.com/firebase/angularfire/issues/876 –

回答

2

你可以得到標識的孩子,想盡Object通過Firebase返回包含$id屬性

修訂如下

console.log("The new status is: "+ player.$id); 

你可以得到id/key甚至Firebase不返回ID在孩子當中,請嘗試以下

console.log("The new status is: "+ data.ref.key); //it will returns -KV_Cj7sL6sg6K9E5ifh 


console.log("The new status is: "+ data.ref.parent.key); //it will returns driver 
+0

扎伊德它給了我不確定。這裏是我如何使用它'usersRef.on(「child_changed」,函數(數據)var player = data.val(); console.log(「新狀態是:」+ player.status); console .log(「新的ID是:」+ player。$ id); }); ' – Gardezi

+0

'console.log(player)'的輸出是什麼? –

+0

Zaid它是undefined – Gardezi

2

您稱爲id的稱爲密鑰以Firebase的名詞。爲了得到一個快照的關鍵,您可以訪問快照的key屬性:

usersRef.on("child_changed", function(snapshot) { 
    var player = snapshot.val(); 
    console.log(player); 
    console.log("The new status is: "+ player.status); 
    console.log("The key of the player is "+snapshot.key); 
}) 
+0

我知道。我試着用鑰匙,但那不適合我。這真的讓我感到驚訝,因爲我來到這裏 – Gardezi

+0

弗蘭克它開始像我說的那樣爲我工作。我試圖找出問題所在,但卻無法找到任何問題。重新啓動計算機,我就像你告訴我的那樣獲得了鑰匙。相當奇怪。 – Gardezi

+0

很高興聽到你排序。 –