0
  var relative = af.database.object('users/user75ECZOiNtxZwYoezaXmYA9YwPm53', { preserveSnapshot: true });
  relative.subscribe(
    snapshot => {
      this.usedBasicProfile = snapshot;
    }
  );
  console.log(this.usedBasicProfile);  //the value is undefined, how to make this work?

what I want to do is get the data from the firebase, anyone could help me?

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
pyy
  • 915
  • 3
  • 9
  • 25

1 Answers1

1

It appears you're using an async method which means the value is not available until execution gets inside the callback handler. Have you tried this?

var relative = af.database.object('users/user75ECZOiNtxZwYoezaXmYA9YwPm53', { preserveSnapshot: true });

relative.subscribe(snapshot => {
    this.usedBasicProfile = snapshot;
    console.log(this.usedBasicProfile);
});
d512
  • 32,267
  • 28
  • 81
  • 107
  • I am sorry, your code is still not work for me, but thanks for your help – pyy Apr 06 '17 at 03:38
  • What does it print out? Still undefined? Does `snapshot` have an actual value? – d512 Apr 06 '17 at 03:45
  • yes, bro, I am afraid it is still undefined, while the console.log(snapshot) appear to be like this "V {A: O, V: U, g: rc}" , anyway thanks for your help – pyy Apr 06 '17 at 04:05
  • 1
    So you assign the value of `snapshot` to `this.usedBasicProfile` but by the very next line somehow the value is gone? In a single threaded environment like javascript that's hard to understand. Not sure what's going on there. – d512 Apr 06 '17 at 04:12
  • this second line in your the curly braces worked actually with value , but outside the curly braces, this.usedBasicProfile is undefined, maybe I misunderstand your meaning, sorry. – pyy Apr 06 '17 at 04:49
  • @Steveeeeee Did you read the link echonax gave you? You get your answer there, why it is undefined outside the callback. Please also consider accepting the duplicate, because it's an exact duplicate of that question ;) – AT82 Apr 06 '17 at 16:21