1

I'm using Cordova and Firebase to create a application.

One of the many function I've made is this function:

function searchForGameRound() {
var ref = new Firebase("https://grootproject.firebaseio.com/Users/");

// Dit is de query naar Firebase toe (kan je vergelijken met een query als bij SQL)
var query = ref.orderByChild("gameSearching").equalTo(true);
console.log('spelen');

// Hierbij geef ik aan dat hij dit enkel 1x mag doen. Dit voorkomt dat hij dit op elk child gaat uitvoeren (wat meerdere games maakt).
query.once("value", function (data) {
    console.log('spelen', data.val());

    if (data.val() === null) {
        updateData(('Users/' + you + '/'), {gameSearching: true});
        return;
    }
    var alreadyInCurrentGame;

    var object = data.val();
    var user1 = Object.keys(object)[0];
    var user2 = Object.keys(object)[1];

    // Als je het zelf bent
    if (you == user1) {
        if (user2) {
            opponent = user2;
        } else {
            updateData(('Users/' + you + '/'), {gameSearching: true});
            return;
        }
    } else {
        opponent = user1;
    }

    console.log(opponent, 'opponent found');

    // Hierbij gaat hij kijken of er al een currentgame bestaat met deze user
    var ref = new Firebase('https://grootproject.firebaseio.com/Users/' + you + '/currentGames/' + opponent + '/');
    ref.once("value", function (snapshot) {
        if (!snapshot.val() === null) {
            console.log(snapshot.val(), ' bestaat al');
            alreadyInCurrentGame = true;
        }
    }, function (errorObject) {
        console.log(errorObject.error);
    });

    // Als alles goed is, gooit hij er data in.
    if (!alreadyInCurrentGame) {
        console.log('spel gevonden');


        var game = new Firebase("https://grootproject.firebaseio.com/Games/");
        game.on("value", function (data) {

            var length = Object.keys(data.val()).length;

            for (var i = 0; i < length; i++) {
                games.push(Object.keys(data.val())[i]);
            }

            game1 = Math.floor(Math.random() * 4);
            game2 = Math.floor(Math.random() * 4);
            game3 = Math.floor(Math.random() * 4);

            while(game1 == game2){
                game2 = Math.floor(Math.random() * 4);
            }

            while(game2 == game3){
                game3 = Math.floor(Math.random() * 4);
            }

            console.log(games, 'games');
            game1 = games[game1];
            game2 = games[game2];
            game3 = games[game3];
            console.log(game1);
            console.log(game2);
            console.log(game3);

            var url = 'https://grootproject.firebaseio.com/Users/' + opponent + '/currentGames/' + you + '/';
            var fir = new Firebase(url);
            fir.set(
                {

                    round1: {score: '-', game: game1},
                    round2: {score: '-', game: game2},
                    round3: {score: '-', game: game3}
                }
            );

            url = 'https://grootproject.firebaseio.com/Users/' + you + '/currentGames/' + opponent + '/';
            fir = new Firebase(url);
            fir.set(
                {
                    round1: {score: '-', game: game1},
                    round2: {score: '-', game: game2},
                    round3: {score: '-', game: game3}
                }
            );

            url = 'https://grootproject.firebaseio.com/Users/' + you + '/';
            fir = new Firebase(url);
            fir.update(
                {gameSearching: false}
            );

            console.log(opponent, ' opponent');
            url = 'https://grootproject.firebaseio.com/Users/' + opponent + '/';
            fir = new Firebase(url);
            fir.update(
                {gameSearching: false}
            );
        });

    } else {
        updateData(('Users/' + you + '/'), {gameSearching: true});
    }
}, function (err) {
    console.log(err, 'error');
});

}

My Firebase Rules:

{
  "rules": {
    ".read": true,
    ".write": true,
    "Users": {
      ".indexOn": "gameSearching"
    }
  }
}

It seems that the console.logs are not showing whenever he's using Firebase (the function also wont do it's job).

The only console.log I get is the first console.log('spelen'). What is strange for me because on my PC it works fine.

This function is not the only one that isn't working properly. Every time I'm using Firebase to retrieve some data on my phone, it won't work.

I'm using the newest version of Cordova and Firebase version 2.0.4

Any idea?


EDIT

It seems to work only when I login into the application. When I'm already logged in into firebase (firebase.getAuth()), the function wont work and I wont retrieve any data from firebase.

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
Mr. Sam
  • 373
  • 1
  • 5
  • 17
  • Hoi Sam, I assume it's only the first 'spelen' log that shows up. If that is the case, no data is coming back from Firebase. Did you whitelist your Firebase, like described over here? http://stackoverflow.com/questions/22383281/why-is-firebase-returning-404-errors-in-phonegap-app-when-running-on-android-emu?lq=1 – Frank van Puffelen Jan 16 '15 at 14:20
  • Perhaps firebase js is not loaded (yet) correctly, so you are getting an exception in your ref.orderByChild line? – mentat Jan 16 '15 at 14:20
  • @mentat: in that case the first `console.log('spelen')` would not execute, because there would be an error before it. – Frank van Puffelen Jan 16 '15 at 14:22
  • I did not whitelist myFirebase. I'lll try that, thx! – Mr. Sam Jan 16 '15 at 14:29
  • Hmm unfortunately it didn't made any difference. You where right btw Frank van Puffelen, only the first 'spelen' shows up. – Mr. Sam Jan 16 '15 at 14:36
  • Your edit points to security rules. Do you have customized security rules? If so, please add them to the question. – Frank van Puffelen Jan 16 '15 at 15:49
  • 1
    Try enabling Firebase logging with `Firebase.enableLogging(true, true)` in your client code, and share any interesting findings here. – Rob DiMarco Jan 16 '15 at 17:07
  • Seems that firebase is not making a connection... When I login it says a lot of things, including; connection ready. But when I close the app and re-open it, it's not making any connection, only telling me; "p:0: Listen called for /Users/sam/currentGames default". Do I need to make the connection everytime I open my application? This seems weird because of the Session length option I've in Firebase. – Mr. Sam Jan 18 '15 at 21:54
  • I found something else interesting. When I try to logout, the firebase says "p:0: Browser went online. Reconnecting. ". So it seems that firebase won't recognize that the browser is online. Firebase starts making a connection when the browser is coming online, and that's only happening when using authWithPassword or unauth – Mr. Sam Jan 18 '15 at 22:16

1 Answers1

1

Seems firebase was not making any connection without using the function authWithPassword or unauth.

I solved my problem by using Firebase.goOnline(); whenever the application was started;

document.addEventListener("deviceready", function () { 
    Firebase.goOnline();
}
Mr. Sam
  • 373
  • 1
  • 5
  • 17