FireBase规则indexOn问题

时间:2018-01-24 07:35:00

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

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

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

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

  

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

我在FireBase中的规则是

#include <stdio.h>
#include <stdlib.h>

#include "./list.h"

int main(int argc, char *argv[]) {
    int arr[] = {1,2,3,4};
    List *list = createList();
    int i = 0;
    for(i = 0; i < 4; i++) {
        appendList(list, arr[i]);
    }
    return 0;
}

List *createList() {
    List *list = malloc(sizeof(List));
    if(list == NULL) {
        return NULL;
    }

    list->head = malloc(sizeof(Node));
    list->tail = malloc(sizeof(Node));
    if(list->head == NULL || list->tail == NULL) {
        free(list);
        return NULL;
    }

    list->size = 0;
    return list;
}

void appendList(List *list, int num) {
    if(list->head->value == 0) {
        list->head->value = num;
        list->tail->value = num;
        return;
    }
    Node *current = calloc(1, sizeof(Node));
    current = list->head;
    while(current->next != NULL) {
        current = current->next;
    }
    current->next = calloc(1, sizeof(Node));
    if(current->next == NULL) {
        free(current->next);
        printf("Failed to allocate memory");
        exit(1);
    }
    current->next->value = num;
    list->size += 1;
    list->tail = current->next;
}

这是我的用户表截图 enter image description here

1 个答案:

答案 0 :(得分:2)

您应该从".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);
        }
    }
});