child_changed事件firebase
我对firebase很陌生。 当我遇到一些非常酷的事件时,我只是试着举一些例子。
我已经在firebase数据库中保存了一些数据:
driver -KV_Cj7sL6sg6K9E5ifh status: "Incomplete"
麻烦我的是,当一个孩子更新child_changed事件触发。 当我访问它的数据时,我没有看到它的关键。
我在驱动程序表中创build了一个节点(我认为在firebase中有一行称为不确定的节点)。 当我改变它的状态来完成它触发的child_changed事件。 我收到已更新的数据包。 事情是我想要得到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); });
请帮助我,我将如何收到编号。 谢谢
你可以得到身份证的孩子,尝试下面
console.log("The new status is: "+ player.$id);
Firebase
返回的每个Object
包含$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
你称之为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); })