【问题标题】:FireBase Rules indexOn IssueFireBase 规则索引问题
【发布时间】:2018-07-03 04:19:42
【问题描述】:

我正在尝试使用下面的 firebase 查询获取用户的所有电子邮件

private void searchUserByEmail(final String searchText) {  
        DatabaseReference mFirebaseDatabaseReference = FirebaseDatabase.getInstance().getReference();
        Query query = mFirebaseDatabaseReference.child(Constants.FB_TABLE_USERS).orderByChild(Constants.FB_EMAIL).equalTo(searchText);
        query.addListenerForSingleValueEvent(new ValueEventListener() {
}}

在搜索任何不在列表中的电子邮件时,我在控制台中收到一条警告消息,例如

W/PersistentConnection: pc_0 - 使用未指定的索引。考虑将 table_users 中的 '".indexOn": "email"' 添加到您的安全和 Firebase 数据库规则中以获得更好的性能

而我在 FireBase 中的规则是

{
  "rules": {
    ".read" : "auth != null",
    ".write" : "auth != null",
    "table_users": {
      ".indexOn": ["email"]
    }
  }
}

这是我的用户表截图

【问题讨论】:

  • 试试这个".indexOn": "email"
  • @Ali 是的,我也尝试过这种方式,但我没有得到 firebase 的任何回应
  • “我没有得到 firebase 的任何响应” 你的意思是没有显示警告吗?
  • 你的意思是警告没有显示-->是
  • 然后就是这样,你在电子邮件上做一个索引

标签: android firebase firebase-realtime-database firebase-security


【解决方案1】:

您应该从 ".indexOn" 值中删除括号

"table_users": {
  ".indexOn": "email"
}

query.addListenerForSingleValueEvent(new ValueEventListener() {

    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
        for (DataSnapshot snapshot : dataSnapshot.getChildren()) {
            String email = snapshot.child("email").getValue();
            System.out.print(email);
        }
    }
});

【讨论】:

  • 但它没有进入 OnDataChanges 方法
  • 尝试将"bbb@gmail.com" 传递给searchUserByEmail 方法,你现在得到了什么?
  • 谢谢它运行良好你节省了我的时间:) ForGot this condtion in OnDataChnaged() if (dataSnapshot != null && dataSnapshot.getValue() != null) { }
猜你喜欢
  • 2018-06-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-10-28
  • 2021-06-13
相关资源
最近更新 更多