Firebase没有定义索引 [英] Firebase no index defined

查看:124
本文介绍了Firebase没有定义索引的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Firebase来检索用户的一些数据。
我只需要用( gameSearching = true )检索只有一个用户

我的数据如下所示:

 用户
|
| _____约翰
| | ___名称:John Farmer
| | ___ gameSearching:true
|
| _____ James
| | ___名字:詹姆斯·史密斯
| | ___ gameSearching:false
|
| _____ Barbara
| ___ name:Barbara Smith
| ___ gameSearching:true

我正在执行这段代码:

  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();
$ b $ setData(('Users /'+ opponent +'/'),{gameSearching:false});
setData (('Users /'+ yourself +'/'),{gameSearching:false});
}
}
);


} );因为数据有多个值,所以我使用data.forEach来分隔它们。在这之后,我可以在'true'上设置'return',所以我只会收到一个'gameSearching:true'的用户。我的firebase规则:

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

运行此代码时,出现以下错误:

lockquote
未捕获错误:没有为gameSearching定义的索引'

我使用'.indexOn'搜索了'gameSearching'的索引,但没有成功。
https://www.firebase.com/docs/security /guide/indexing-data.html



有没有人有一个例子或解决方案?






编辑:

现在我得到了下面的代码:

  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);
$ b $ setData (('Users /'+ opponent +'/'),{gameSearching:false});
setData(('Users /'+ yourself +'/'),{gameSearching:false});
console.log('ja');
});

我修改了我的firebase规则:

<
rules:{$ b $.read:true,
.write:true,
Users :{
.indexOn:gameSearching
}
}

}



indexOn错误不会再显示,所以这很好。但是 ref.orderByChild(gameSearching)。equalTo(true).limitToFirst(1 )将运行两次。我不知道这是可能的。我使用了limitToFirst(1)`,所以我期待1个孩子?

解决方案

现在你添加了规则清除那里有什么问题。



以下是您目前的规则:

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

$ / code>

因此,您允许所有人在您的整个Firebase上读取和写入访问权限并告诉它在 gameSearching 上添加索引。问题是,您已经在顶层定义了索引,而不是在Firebase中需要的级别。



这是您当前的数据结构:

  {
Score:{
sam_lous:{
得分:2


用户:{
hallo:{
gameSearching:true
},
sam_lous:{
gameSearching:true
},
test:{
gameSearching:false
},
test2:
gameSearching:true


$ b $ $ $
$ b gameSearching 属性仅存在于 Users 节点的子项下。因此,您应该在这里定义您的 .indexOn

  {
规则:{
.read:true,
.write:true,
Users:{
.indexOn:gameSearching





$ b有了这个,你的指数应该创建(根据我的经验,当你保存你的规则时,它会立即发生),错误/警告消息应该消失。但是为了解决这个问题,你也应该对我的答案进行修改。



我不得不承认Firebase有关索引数据的文档可能会更清晰地定义这些 .indexOn 规则。如果Firebase中的某个人正在阅读,您可以添加一些更好的示例,强调 .indexOn 需要在数据所在的级别定义。 / p>

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?

解决方案

Now that you added your rules, it is clear what is wrong there.

These are your current rules:

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

So you allow everyone read and write access on your entire Firebase and tell it to add an index on gameSearching. The problem is that you've defined your index at the top-level, instead of at the level where it needs to be in your Firebase.

This is your current data structure:

{
    "Score" : {
        "sam_lous" : {
            "score" : 2
        }
    },
    "Users" : {
        "hallo" : {
            "gameSearching" : true
        },
        "sam_lous" : {
            "gameSearching" : true
        },
        "test" : {
            "gameSearching" : false
        },
        "test2" : {
            "gameSearching" : true
        }
    }
}

The gameSearching property exists only under children of the Users node. So that is where you should define your .indexOn:

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

With that, your index should be created (in my experience it happens straight away when you save your rules) and the error/warning message should disappear. But to fix the behavior, you should also make the changes that I put in my other answer.

I have to admit that Firebase's documentation on indexing your data, could be clearer on where you define these .indexOn rules. If someone at Firebase is reading along, can you please add some better examples that highlight that .indexOn needs to be defined at the level where the data resides?

这篇关于Firebase没有定义索引的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆