-1

I'm attempting to access a variable from within a datasnapshot:

  getEmail() {
    var test = firebase.database().ref('/users/' + user).once('value')
    .then(function(snapshot) {
        var email = snapshot.val().email;
        });
        console.log(email);
    }

That console.log doesn't return the database value, and I am also unable to access that email variable elsewhere in my code.

AL.
  • 36,815
  • 10
  • 142
  • 281
Billy Bob Joel
  • 199
  • 2
  • 4
  • 15
  • Move the log statement up into the scope of the resolved promise. `email` is only defined inside the scope there, so it's only accessible there. – Sterling Archer Jun 06 '17 at 23:55
  • 2
    Possible duplicate of [How do I return the response from an asynchronous call?](https://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call) – Sterling Archer Jun 06 '17 at 23:55
  • So how would I be able to access the email value outside the scope? Is there another firebase function I could use? – Billy Bob Joel Jun 07 '17 at 00:47
  • You can't (in any sensible way) make the value accessible outside of the `then` callback. In other words, everything that relies on that value has to be handled from the `then` callback. The reason is that the value is fetched asynchronously and for the outside scope, which is executed synchronously, to have access to it would require Javascript to have the ability to time travel. – Lennholm Jan 15 '19 at 18:14

2 Answers2

0

there are 2 possible solutions: (both mentioned by Sterling in comments)

1) the log statement is not in the scope of the .then method:

getEmail () {
  var test = firebase.database().ref('/users/' + user).once('value')
    .then(function (snapshot) {
      var email = snapshot.val().email
      console.log(email)
    })
}

2.) you could return the email variable in the .then method and log the test variable:

getEmail () {
  var test = firebase.database().ref('/users/' + user).once('value')
    .then(function (snapshot) {
      var email = snapshot.val().email
      return email
    })
  console.log(test)
}

**) clearest way IMO is like this:

getEmail () {
  var email = firebase.database().ref('/users/' + user).once('value')
    .then(function (snapshot) {
      return snapshot.val().email
    })
  console.log(email)
}
sgraham
  • 143
  • 8
  • The console.log returns the following object: Promise {_45: 0, _81: 0, _65: null, _54: null}. Under the _65 key is where the data I want is stored. Is there a reason the keys are showing up like this? – Billy Bob Joel Jun 07 '17 at 01:12
  • try JSON.stringify(email) in the log statement – sgraham Jun 07 '17 at 02:17
  • also i am guessing you are trying to access the value for the email key which i believe should be as such `return snapshot.child('email').val()` – sgraham Jun 07 '17 at 02:26
-2

getEmail () {
  var email = firebase.database().ref('/users/' + user).once('value')
    .then(function (snapshot) {
      return snapshot.val().email
    })
  console.log(email)
}
shashank
  • 65
  • 6
  • That'd set `email` equal to a Promise that would resolve at a later time, not get the actual value from the response – dan Jan 15 '19 at 18:46