Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
160 views
in Technique[技术] by (71.8m points)

javascript - Firebase no index defined

I'm using Firebase to retrieve some data of Users. I need to retrieve only one User with (gameSearching = true).

My data looks like:

Users
|
|_____ John
|        |___ name: John Farmer
|        |___ gameSearching: true
|
|_____ James
|        |___ name: James Smith
|        |___ gameSearching: false
|
|_____ Barbara
         |___ name: Barbara Smith
         |___ gameSearching: true

I'm running this code:

setData(('Users/' + you + '/'), {gameSearching: true});

var ref = new Firebase("https://grootproject.firebaseio.com/Users");
var child = ref.orderByChild("gameSearching");
child.on("child_added", function (data) {
    var yourself = firebase.getAuth().password.email.split("@", 1)[0];
    data.forEach(
        function (childSnapshot) {
            if (childSnapshot[i].val().gameSearching == true && childSnapshot[i].key() != yourself) {
                var opponent = childSnapshot[i].key();

                setData(('Users/' + opponent + '/'), {gameSearching: false});
                setData(('Users/' + yourself + '/'), {gameSearching: false});
            }
        }
    );


});

Because 'data' has multiple values, I use 'data.forEach' to separate them. After that point I can set a 'return' on 'true' so I only will recieve one User with 'gameSearching: true'.

My firebase rules:

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

When I run this code I get the following error:

'Uncaught Error: No index defined for gameSearching'

I have searched for setting a index on 'gameSearching' using '.indexOn' but not with any succes. https://www.firebase.com/docs/security/guide/indexing-data.html

Does anyone have a example or a solution?


Edit:

I now got the following code:

var ref = new Firebase("https://grootproject.firebaseio.com/Users");
    ref.orderByChild("gameSearching").equalTo(true).limitToFirst(1).on("child_added", function (data) {
        var yourself = firebase.getAuth().password.email.split("@", 1)[0];

            var opponent = data.key();
            console.log(opponent);

            setData(('Users/' + opponent + '/'), {gameSearching: false});
            setData(('Users/' + yourself + '/'), {gameSearching: false});
            console.log('ja');
    });

I edited my rules of my firebase:

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

}

The indexOn error wont show anymore, so that's great. But ref.orderByChild("gameSearching").equalTo(true).limitToFirst(1) will runs twice. I don't know how that's possible. I'm using limitToFirst(1)`, so I expect 1 child?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...