2

I'm new to Firebase and Node.js, but what I'm trying to do is the following:

gameScoreRef.child(score["ss"][i][10]).once('value', function(snapshot) {
    if (!snapshot.val()) {
        // NULL
    } else {
        gameID = snapshot.val();
        console.log(gameID);
    }
});

var ref = gamesRef.child(gameID);
ref.update({
    aScore: parseInt(score["ss"][i][5]),
    hScore: parseInt(score["ss"][i][7])
});

So basically I am making one Firebase call to get the GameScore Ref and once that is retrieved, which works perfectly, it will use that GameID to update the game score.

The error that is thrown is that the gameID is null, and not because Firebase actually has null, the value is displayed correctly with the console.log(gameID) as 43, its because it seems that the first Firebase call has not finished before the second Firebase call, to update the scores is reached?

How would I resolve that?

AL.
  • 36,815
  • 10
  • 142
  • 281
Learn2Code
  • 1,974
  • 5
  • 24
  • 46

1 Answers1

1

Move the code below gameScoreRef.child inside it's callback. gameScoreRef.child is an asynchronous function, gameID doesn't exist when you're using it in gamesRef.child(gameID).

 gameScoreRef.child(score["ss"][i][10]).once('value', function(snapshot) {
        if (!snapshot.val()) {
            // NULL
        } else {
            gameID = snapshot.val();
            console.log(gameID);
            var ref = gamesRef.child(gameID);
            ref.update({
               aScore: parseInt(score["ss"][i][5]),
               hScore: parseInt(score["ss"][i][7])
            });
        }



    });

    // "gameID" doesn't exist here!

Or you can make a retrieveScore function that returns a promise.

function retrieveScore(ref){

    return new Promise((resolve, reject) => {
        gameScoreRef.child(ref).once('value', snapshot => {
            if (!snapshot.val()) {
                return reject();

            resolve(snapshot.val());

        });
    });

}


retrieveScore(score["ss"][i][10]).then(gameID => {
    console.log(gameID);
    let ref = gamesRef.child(gameID);
    ref.update({
        aScore: parseInt(score["ss"][i][5]),
        hScore: parseInt(score["ss"][i][7])
    });

}).catch(err => {
   //Handle error
});

Check this questions:

Community
  • 1
  • 1
Marcos Casagrande
  • 37,983
  • 8
  • 84
  • 98
  • case 1: this causes the same issue with gameID being null at that time of the call. – Learn2Code Apr 20 '17 at 17:04
  • @learningiOS check my edited answer, `gamesRef.child(gameID)` is inside the `else` block, is the `console.log` is being printed correctly? – Marcos Casagrande Apr 20 '17 at 17:07
  • Can you add a console.log inside `catch` and tell me if anything is printed there? – Marcos Casagrande Apr 20 '17 at 17:12
  • I can't test your code, but if `gameScoreRef.child` works as you put in your code sample, my code should work. – Marcos Casagrande Apr 20 '17 at 17:14
  • I coded it the way you have with the correction where you did not complete the if else pairing; but it still does not work. In the retrieveScore function the gameScoreRef.child(ref).once(... never gets called!? – Learn2Code Apr 20 '17 at 20:24
  • @learningiOS if `once` never gets called there isn't a problem with my code, there is a problem with your code (which you didn't show). My code works, tested it serveral times. So please provide a little more code so we can help you. – Marcos Casagrande Apr 20 '17 at 20:32
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/142216/discussion-between-marcos-casagrande-and-learningios). – Marcos Casagrande Apr 20 '17 at 21:06
  • i put some error code and i get the follwoing: TypeError: Cannot read property '5' of undefined at getScores.then.gameID (/Users/sguerrera/NodeJS/getnflscores:216:60) at process._tickCallback (internal/process/next_tick.js:109:7) – Learn2Code Apr 20 '17 at 21:26