简体   繁体   中英

Cordova with Firebase won't work

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.

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();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM