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