使用Firebase的Cordova无法使用

时间:2015-01-16 14:06:34

标签: javascript android cordova firebase

我使用Cordova和Firebase创建应用程序。

我所做的众多功能之一就是这个功能:

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

}

我的Firebase规则:

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

似乎console.logs在他使用Firebase时没有显示(该功能也不会这样做)。

我得到的唯一的console.log是第一个console.log('spelen')。对我来说有什么奇怪的,因为在我的电脑上工作正常。

此功能不是唯一一个无法正常工作的功能。每当我使用Firebase在手机上检索某些数据时,它都无法正常工作。

我使用的是最新版本的Cordova和Firebase版本2.0.4

有什么想法吗?


修改

它似乎仅在我登录应用程序时才有效。当我已经登录到firebase(firebase.getAuth())时,该函数将无法工作,我将无法从firebase中检索任何数据。

1 个答案:

答案 0 :(得分:1)

似乎没有使用authWithPassword或unauth函数,firebase没有建立连接。

我在应用程序启动时使用 Firebase.goOnline(); 解决了我的问题;

document.addEventListener("deviceready", function () { 
    Firebase.goOnline();
}