I am trying to get the key from a child nodes data.
$(document).on('click', '#enter', getName);
function getName() {
name = $('#user').val().trim();
database.ref('players/' + name).push({
name: name
})
}
database.ref('players/' + name).child(name).on("child_added",
function(childSnapshot){
var childKey = childSnapshot.key;
var childData = childSnapshot.val;
console.log('key', childKey)
console.log('data', childData)
}, function (errorObject) {
console.log("The read failed: " + errorObject.code);
})
})
In the snapshot function, when I console log the childKey
, it shows the key is the name of the user, which is the path I created in the ref('players/' + name)
. But I need the key for the node that gets nested inside the name
node. I get an error in my childSnapshot
function saying that the .child(name) is an invalid path
. Also says,
Paths must be non-empty strings and can't contain ".", "#", "$", "[", or "]"
Currently when I create data onto Firebase it looks like this:
-players
-Johnson
-KWH4mjWbOuptod_vV1o
name: "ajks"
I need to get the key under johnson
, that long string of letter and numbers so I can reference that specific node and add data, change data or update data such as the name.