5

I'm trying to get the data from my Firebase with AngularFire2. I want to check specific data and after I get this data from Firebase, I can check it only in the specific scope and not after the operation to Firebase. Why does it happen?

Below is my code:

 this.af.database.list('/users/1qfcMAQnglX9jsW5GdLpPko1HqE2', { preserveSnapshot: true})
.subscribe(snapshots=>{
    snapshots.forEach(snapshot => {
      if(snapshot.key=="reg_boolean"){
        console.log(snapshot.val());
        this.bo=snapshot.val();
      }
      this.currentUser.push({key:snapshot.key,value:snapshot.val()});
      console.log(this.currentUser);
      //console.log(snapshot.key, snapshot.val());
      if(this.bo==true){console.log("happy"); }; //i can access only in this scope
    });

})
 if(this.bo==true){console.log("happy"); }; //why i can access this value??it's undefined, this happen before the subscribe with angularfire2

enter image description here

AL.
  • 36,815
  • 10
  • 142
  • 281
Manspof
  • 598
  • 26
  • 81
  • 173
  • 1
    Victor's answer explains, but I'll also refer you to these reference questions: [How do I return the response from an asynchronous call?](http://stackoverflow.com/q/14220321/), [Variables set during $.getJSON function only accessible within function](http://stackoverflow.com/q/1739800) and [jQuery: Return data after ajax call success](http://stackoverflow.com/1/5316697). – Frank van Puffelen Jan 15 '17 at 16:22

1 Answers1

8

It's undefined because this.af.database.list it's asynchronous so the code in subscribe will execute when this.af.database.list does retrieve the data. So when the code got to the line if(this.bo==true){console.log("happy"); }; It has nothing because subscribe did not finish at all.

The subscribe it's like the old promise but now it's working with rxjs I recommend you to learn it because angular and ionic has a lot of focus on that.

Try looking at https://www.learnrxjs.io/

Victor Godoy
  • 1,642
  • 15
  • 18
  • Victor do you have any solution for me now or anything can solve this issue? because I'm not sure how to work with rxjs. – Manspof Jan 15 '17 at 19:46
  • @AdirZoari what do you want to do with the data? – Victor Godoy Jan 15 '17 at 21:26
  • okay i explain you in general. after sign up in his first time he create account and complete to fill all the required data(department name,year), i set the reg_boolean to true means if next time he enter to app, it checks in data, if the reg_boolean is true so he doesn't need to choose department name and year again so it nav to homepage and not sign up. – Manspof Jan 15 '17 at 22:39
  • Check it https://gist.github.com/vigohe/cedbf896503255d3babb7ebbe1ceb934 – Victor Godoy Jan 15 '17 at 23:19
  • hey victor, i did that and i get the error when i run it TypeError: _this._myService.getRegBoolean(...).filter is not a function – Manspof Jan 16 '17 at 06:48
  • @AdirZoari I did add the missing filter import. – Victor Godoy Jan 16 '17 at 11:55
  • victor, I appreciate your help. now it's compiled but although the reg_boolean is true it's not show mean and not enter to where i nav it. i even print with console.log to see the result but it's not go to it. – Manspof Jan 16 '17 at 13:42
  • @AdirZoari Can you add the template part please. – Victor Godoy Jan 27 '17 at 12:01
  • sure https://gist.github.com/adirzoari/6f8818a7ef95f9276f3576cb2a60da6d – Manspof Jan 27 '17 at 13:19
  • Maybe I didn't explain my self right. I use in firebase as database. each user who signup to my app has reg_boolean attribute in db, if he already finish so i want it to go directly to HomePage, if not so the regBoolean is be False and then he go to signUpPage. did you get the idea? – Manspof Jan 27 '17 at 13:20