0
constructor(private router: Router){
  //Initiate Firebase Connection
    fireApp.auth().onAuthStateChanged(function (user) {
        if (user) {
            fireApp.database().ref('/notifications/' + user.uid).on('value', data => {
                if (data.val()) {
                    this.notificationCount = data.val().notification.length}
            });
        } else {
            this.httpService.fetchData('get', 'v1/notification/auth', function (data) {
                data = data.json();
                try {
                    fireApp.auth().signInWithCustomToken(data.FBToken).then
                         (function (signeduser) {
                         fireApp.database().ref('/notifications/' + signeduser.uid).on
                         ('value', data => {
                            if (data.val()) {
                                this.notificationCount = data.val().length
                         }
                        });
                    }).catch(function (err) {
                        console.log(err);
                        throw err;
                    });
                }
                catch (e) {
                    console.log((<Error>e).message);//conversion to Error type
                }
            }, {});
        }
    });
}

  ngOnInit() {
    this.getData();
}

And then inside getData function I want notificationCount value which I should be receiving from firebase Connection

getData(){
  console.log(this.notificationCount)
 }

I get notificationCount when component loads as event for Firebase connection takes time to return the result(Firebase connection returns the result but after component is rendered), how notificationCount value can be retrieved before the component is rendered using resolve or promise in this scenario, as I haven't used resolve in Angular 2

msmani
  • 730
  • 1
  • 7
  • 24
Enthu
  • 512
  • 2
  • 13
  • 38
  • Possible duplicate of [Angular2: How to load data before rendering the component?](https://stackoverflow.com/questions/35655361/angular2-how-to-load-data-before-rendering-the-component) – msmani Oct 06 '17 at 12:01
  • Please could you give a hint as to how can I apply promise to Firebase event I tried referring to the above query before I had posted this, thanks – Enthu Oct 06 '17 at 15:10

0 Answers0