最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Firebase data is "undefined" when it is not - Stack Overflow

programmeradmin3浏览0评论

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 entire codes 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
 |  Show 2 more ments

1 Answer 1

Reset to default 3

I 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;

        }); 
发布评论

评论列表(0)

  1. 暂无评论