I trying to figure out why i am getting objects in //1 and //2 but getting undefined at //3...
I am messing around Angular2 with Typescript and Firebase back end.
What am i doing wrong here, seems to me like a rookie mistake...
getPlayers() {
var snap;
this.playersRef.once('value', function (snapshot) {
snap = snapshot.val();
//1
console.info("snap.val()" + snapshot.val());
//2
console.info("var snap" + snap);
}
);
//3
console.info("var snap loc 2" + snap);
//return new Promise<Player[]>(resolve => resolve(snap));
}
///////// EDIT /////////////////////////////////////////////
Can someone explain how this pertains to the Firebase nosql DB in particular (Inside of Angualr2 app).
in my app.component.ts file i have this
constructor(private _playerService: PlayerService){}
getPlayers(){
this._playerService.getPlayers().then(res => this.players = res);
}
ngOnInit(){
this.getPlayers();
}
inside the player.service.ts file I have this
getPlayers() {
this.playersRef.once('value', function (snap){
return snap.val();
});
}
I always get TypeError: this._playerService.getPlayers(...) is undefined
I've read some atricle such as this https://www.firebase.com/blog/2016-01-21-keeping-our-promises.html
but i still can't figure out how to make everything work together.
Also when i try to edit my getPlayer()
with promises (insead of callbacks) like in the article i get an exception stating that .once()
needs at least two parameters so I'm not sure how the article is working at all with .once('value').then()