I'm kind of new to firebase. I was just trying out some example when I came across some of its event which by the way are pretty cool.
I have saved some data in firebase database which is :
driver
-KV_Cj7sL6sg6K9E5ifh
status: "Incomplete"
The thing that troubling me is that when a child is updated child_changed event is triggered. When I access it's data I'm not seeing the key with it.
I have made a node(I think a row is called a node in firebase not sure) in the driver table. When I changed the status of it to complete the child_changed event it triggered. And I receive the packet which has been updated. The thing is I want to get the Id(KV_Cj7sL6sg6K9E5ifh) too. But the problem is it's only sending me status: complete.
This is the code that I have written for child_changed:
usersRef.on("child_changed", function(data) {
var player = data.val();
console.log(player);
console.log("The new status is: "+ player.status);
});
Please help me that how will I receive the Id too. Thanks
What you call the id, is called the key in Firebase terms. To get the key of a snapshot, you can access the key property of that snapshot:
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);
})
you can get Id child,try below
console.log("The new status is: "+ player.$id);
every Object returned by Firebase contains $id attribute
UPDATED:
you can get id/key even Firebase does not return id among child, try below
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With