0

I am building a web app with JS and firebase as my DB, but I'm struggeling to call on my gotData function to get it's return value.

var dbRef = firebase.database().ref('testing');

function reading(){
      dbRef.on('value', gotData,errData) ;
      }

function gotData(data){

  var tests = data.val();
  var keys = Object.keys(tests);
  var length = keys.length;
  var k = keys[lengthe-1];
  console.log(k)
  console.log(data)
  return k;
}

function errData(err){
  alert('error')
}

The code works and I get the documentID of the newest document returned, but I fail to call on this value.

I tried:

var fbid = gotData(data);

but I get an data is not defined error. From my understanding data is just used to reference to the Firebase data so I can do operations on it. What do I need to do, so that I can call the function with it's data ?

Fatmetal
  • 5
  • 3
  • That sounds unusual indeed, as the snapshot should always exist in the callback. I don't immediatly see what's wrong in the code you posted. Can you set up a small reproduction on a site like jsbin, so that I can have a look? – Frank van Puffelen Mar 27 '20 at 14:11
  • Here is the jsbin: https://jsbin.com/jenahuh/edit?html,output . This part of code is only to test, once it works I'll implement it into my website. The sole purpose of this function is to get the ID of the newest document in my Firebase(var k in my code) and then use it in another function. Thanks for looking into this :) – Fatmetal Mar 27 '20 at 14:34
  • That's unfortunately not very helpful, as it doesn't actually reproduce the problem. If you're afraid of sharing your project configuration (which [is not needed](https://stackoverflow.com/questions/37482366/is-it-safe-to-expose-firebase-apikey-to-the-public), but still quite common), you can create a temporary project for the jsbin, and then delete that project afterwards. – Frank van Puffelen Mar 27 '20 at 15:11
  • Okay, I will delete it once the problem is solved. This is the whole project, so I hope this helps to find a solution: https://jsbin.com/mewojen/edit?html,output – Fatmetal Mar 27 '20 at 15:35

1 Answers1

0

You're calling gotData() yourself at the bottom of the code in the jsbin, without passing in a parameter:

var id = gotData();

How I found this:

  1. Put this line as the first statements inside gotData():

    if (!data) debugger
    
  2. Run the code in the jsbin again, which starts the debugger now.

  3. Check the call stack (on the right hand side in the Chrome debugger) to see where the call came from.

You'll want to remove this gotData() call, and put all code that needs the data inside the gotData() functions. Also see:

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
  • Okay, thanks. I thought there would be a way to call on the function, putting all the code that needs the data was my backup plan if nothing else works. Thanks for your help. Can I do something else besides giving you an upvote(this is my first post on stack overflow)? – Fatmetal Mar 27 '20 at 17:10
  • 1
    Alright, I will read through the links later this evening. I wrote everything inside the gotData and it works like intended. Thanks for the help :) – Fatmetal Mar 27 '20 at 18:12