Firebase Web - Is it possible to get the value's name?

Multi tool use
Firebase Web - Is it possible to get the value's name?
is there a way for me to retrieve the name of a value?
For example:
I want to get the value names highlighted in yellow.
However, right now I can only get:
From my understanding the below code only return the value which is the player's scores.
var childData = childSnapshot.val();
Can I do something like this to get the value name?
var childValueName = childSnapshot.val().name;
This is my java code:
function PrintData()
{
var ref = firebase.database().ref('Score');
var PrintData = document.getElementById('PrintOutData');
ref.on("value", function(snapshot)
{
PrintData.innerText = "";
snapshot.forEach(function(childSnapshot)
{
console.log("childSnapshot.key: " + childSnapshot.key);
var childData = childSnapshot.val();
let keys = Object.keys(childData)
keys.forEach(key => {
let value = childData[key.toString()];
console.log("value: " + value);
PrintData.innerText += childSnapshot.key + ": " + value +"r";
})
});
});
}
My html code:
<button type="button" onclick="PrintData()">Print!</button>
Please correct me if I am wrong! Thankyou.
value name
tag name
Hi @VicJordan , what's a tag name? In my first picture above, I could only get the scores (numbers) but not the "GamePlay" names. I want to get the entire line!
– Dracarys
Jul 3 at 2:08
Add your html code and JavaScript code as well in question
– VicJordan
Jul 3 at 2:09
You're going to have to show your query so we can understand what exactly
childSnapshot
is.– Doug Stevenson
Jul 3 at 2:13
childSnapshot
Is that not just
key
inside your keys.forEach
?– Joseph Webber
Jul 3 at 2:34
key
keys.forEach
4 Answers
4
Firebase has a well versed and beautiful documentation.
According to firebase documentation, datasnapshot is returned when you pass a relative path to the child() method
// Assume we have the following data in the Database:
{
"name": {
"first": "Ada",
"last": "Lovelace"
}
}
// Test for the existence of certain keys within a DataSnapshot
var ref = firebase.database().ref("users/ada");
ref.once("value")
.then(function(snapshot) {
var name = snapshot.child("name").val(); // {first:"Ada",last:"Lovelace"}
var firstName = snapshot.child("name/first").val(); // "Ada"
var lastName = snapshot.child("name").child("last").val(); // "Lovelace"
var age = snapshot.child("age").val(); // null
});
The following may work for your purpose:
firebase.database().ref('score').once('value', snap => {
var data = snap.val() // should return => {User1: {GamePlay00: 3}, User2:
{GamePlay00: 1}, ...}
var users = Object.keys('data') should return // => [User1, User2, ...]
var usersDatas = users.map(user_id => data[user_id]) // should return something like => [{gamePlay00: 3}, {gamePlay00:1}, ...]
var value = usersDatas.map(game_play_id => game_play_id) // should return => [gamePlay00, gamePlay00...]
})
Please refer to these link for further documentation: Object.keys Firebase Docs
I'd stick to using Snapshot.forEach()
for the lower level too:
Snapshot.forEach()
var ref = firebase.database().ref('Score');
var PrintData = document.getElementById('PrintOutData');
ref.on("value", function(snapshot) {
PrintData.innerText = "";
snapshot.forEach(function(userSnapshot) {
console.log("childSnapshot.key: " + userSnapshot.key);
userSnapshot.forEach(function(gameSnapshot) {
PrintData.innerText += gameSnapshot.key + ": " + gameSnapshot.val() +"r";
})
});
});
See firebase.database.DataSnapshot
key
(string or null)
The key (last part of the path) of the location of this DataSnapshot.
The last token in a Database location is considered its key. For example, "ada" is the key for the /users/ada/ node. Accessing the key on any DataSnapshot
will return the key for the location that generated it. However, accessing the key on the root URL of a Database will return null
.
DataSnapshot
null
// Assume we have the following data in the Database:
{
"name": {
"first": "Ada",
"last": "Lovelace"
}
}
var ref = firebase.database().ref("users/ada");
ref.once("value")
.then(function(snapshot) {
var key = snapshot.key; // "ada"
var childKey = snapshot.child("name/last").key; // "last"
});
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
What do you mean by
value name
? Do you wanttag name
which contains this value?– VicJordan
Jul 3 at 2:04