Gardezi Gardezi - 1 month ago 18
Node.js Question

child_changed event firebase

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

Answer

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
Comments