Thursday 20 April 2017

Race condition with Node.js and Firebase

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?



via learningiOS

No comments:

Post a Comment