Callbacks/Promises in Firebase
<p>I trying to figure out why i am getting objects in //1 and //2 but getting undefined at //3...</p>

<p>I am messing around Angular2 with Typescript and Firebase back end.</p>

<p>What am i doing wrong here, seems to me like a rookie mistake...</p>

<pre><code>    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&lt;Player[]&gt;(resolve =&gt; resolve(snap));
    }
</code></pre>

<p>///////// EDIT /////////////////////////////////////////////</p>

<p>Can someone explain how this pertains to the Firebase nosql DB in particular (Inside of Angualr2 app).</p>

<p>in my app.component.ts file i have this</p>

<pre><code>    constructor(private _playerService: PlayerService){}

    getPlayers(){
        this._playerService.getPlayers().then(res =&gt; this.players = res);
    }

    ngOnInit(){
        this.getPlayers();
    }
</code></pre>

<p>inside the player.service.ts file I have this</p>

<pre><code>getPlayers() {
    this.playersRef.once('value', function (snap){
        return snap.val();
    });
}
</code></pre>

<p>I always get TypeError: this._playerService.getPlayers(...) is undefined</p>

<p>I've read some atricle such as this
<a href="https://www.firebase.com/blog/2016-01-21-keeping-our-promises.html" rel="nofollow">https://www.firebase.com/blog/2016-01-21-keeping-our-promises.html</a></p>

<p>but i still can't figure out how to make everything work together.</p>

<p>Also when i try to edit my <code>getPlayer()</code> with promises (insead of callbacks) like in the article i get an exception stating that <code>.once()</code> needs at least two parameters so I'm not sure how the article is working at all with <code>.once('value').then()</code></p>