I am trying to retrieve some data from my firebase database, but the value I get is "undefined".
This is how I save the data to the database:
var database = firebase.database();
database.ref().push({
mainArray: mainArray,
secondArray: secondArray,
listname: listName,
mainLanguage: mainLanguage,
secondLanguage: secondLanguage,
}, function(error) {
if (error){
stopLoader();
showSnackbar("An error has occured! Please try again later.");
}
This is how I read the data, but the value of listname is "undefined":
var database = firebase.database().ref().child('codes');
var codeInput = document.getElementById('mainSearch');
database.on('value', function(snapshot) {
if (!snapshot.hasChild(codeInput.value)) {
codeInput.value = "";
showSnackbar("A list with this code does not exist!<br><br>Please try another one.")
}
else {
var data = snapshot.val();
var listname = data.listname;
console.log(listname);
}
});
This is the value I get from the database:
This is how the data is structured in the database:
I am trying to retrieve some data from my firebase database, but the value I get is "undefined".
This is how I save the data to the database:
var database = firebase.database();
database.ref().push({
mainArray: mainArray,
secondArray: secondArray,
listname: listName,
mainLanguage: mainLanguage,
secondLanguage: secondLanguage,
}, function(error) {
if (error){
stopLoader();
showSnackbar("An error has occured! Please try again later.");
}
This is how I read the data, but the value of listname is "undefined":
var database = firebase.database().ref().child('codes');
var codeInput = document.getElementById('mainSearch');
database.on('value', function(snapshot) {
if (!snapshot.hasChild(codeInput.value)) {
codeInput.value = "";
showSnackbar("A list with this code does not exist!<br><br>Please try another one.")
}
else {
var data = snapshot.val();
var listname = data.listname;
console.log(listname);
}
});
This is the value I get from the database:
This is how the data is structured in the database:
Share Improve this question edited Mar 3, 2017 at 4:32 AL. 37.8k10 gold badges147 silver badges285 bronze badges asked Mar 2, 2017 at 18:45 MagnusMagnus 1,0853 gold badges11 silver badges21 bronze badges 7- Can you set up a jsbin that reproduces this problem? – Frank van Puffelen Commented Mar 2, 2017 at 20:35
-
You simply appear to be missing
var data = snapshot.val()[codeInput]
.snapshot.val()
will return your entirecodes
object – Phil Commented Mar 3, 2017 at 4:37 -
@Phil I can retrive everything inside the codes object by using
var data = snapshot.val();
, but I want to retrieve each single Item and make a variable out of them. – Magnus Commented Mar 3, 2017 at 7:49 - @FrankvanPuffelen I will try do do that. – Magnus Commented Mar 3, 2017 at 7:49
- @Magnus in addition to ment from Phil you are also saving to '/' (the root of the DB) but retrieving from /codes/ – ostergaard Commented Mar 7, 2017 at 9:11
1 Answer
Reset to default 3I changed my code to this and now it works perfectly. The problem was that i was trying to get the value from the wrong child. Thanks for your help and patience!
var database = firebase.database().ref().child('codes');
var codeInput = document.getElementById('mainSearch');
database.child(codeInput).on('value', function(snap) {
var data = snap.val();
var listName = data.listname;
var mainLanguage = data.mainLanguage;
var secondLanguage = data.secondLanguage;
var mainArray = data.mainArray;
var secondArray = data.secondArray;
});