Firebase警告:使用Firebase Cloud Function搜索数据时,使用未指定的索引 [英] Firebase warning: Using an unspecified index when search data with Firebase Cloud Function

查看:60
本文介绍了Firebase警告:使用Firebase Cloud Function搜索数据时,使用未指定的索引的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我构建了一个Firebase Cloud Function,该功能查找'IsNotificationEnabled'值等于true的用户. 我功能的一部分

I built a Firebase Cloud Function that looks for users who has value of 'IsNotificationEnabled' equal to true . Part of my Function

export const sendPushNotification = functions.https
.onRequest(async (req, res) => {
    try {
        const { message } = req.body;
        var usersRef = firebase.ref('/Users');
        var usersPushNotificationTokensRef = firebase.ref('/PushNotificationTokens')

        var listUsersSendNotifications = [],
            usersWithTokenSendNotifications = [],
            promises = [];
        if (message) {
            await usersRef.orderByChild('IsNotificationEnabled').equalTo(true).once('value', (snapshot) => {
                snapshot.forEach((childSnapshot) => {
                    console.log(childSnapshot.key)
                    listUsersSendNotifications.push(childSnapshot.key)

                    return false;
                })
            })
            ..........................

正如您在这里看到的那样,我正在寻找IsNotificationEnabled = true的用户.当我运行它时,我会进入日志

as you can see here I'm looking users where IsNotificationEnabled = true. when I run it I get in logs

[2018-05-22T09:12:57.352Z] @ firebase/数据库:FIREBASE警告: 使用未指定的索引.您的数据将被下载并过滤 在客户端上.考虑在以下位置添加".indexOn":"IsNotificationEnabled" /用户遵守您的安全规则,以获得更好的性能.

[2018-05-22T09:12:57.352Z] @firebase/database: FIREBASE WARNING: Using an unspecified index. Your data will be downloaded and filtered on the client. Consider adding ".indexOn": "IsNotificationEnabled" at /Users to your security rules for better performance.

我在Firebase控制台中插入的规则

the rules I insert in firebase console

    {
  "rules": {
     ".indexOn": ["IsNotificationEnabled"],

    "Users":{
      "$uid":{
      ".indexOn": ["IsNotificationEnabled"],
        ".write": "$uid === auth.uid",
        ".read": "$uid === auth.uid"
      }


    },
    ".read": true,
    ".write": true
  }
}

推荐答案

如消息所示,您需要将索引添加到/Users.现在,您将其添加的级别降低了一层,将无法正常工作.

As the message says, you'll need to add the index to /Users. You're now adding it one level lower, where it won't work.

{
  "rules": {
    "Users":{
      ".indexOn": ["IsNotificationEnabled"]
    },
    ".read": true,
    ".write": true
  }
}

通过考虑在哪里运行查询,我发现最容易记住在哪里定义索引.由于您的查询是在/Users上运行的,因此您需要在该级别上定义索引.

I find it easiest to remember where to define an index by thinking of where I run my query. Since your query runs on /Users, you need to define your index on that level.

我也删除了/Users/$uid上的.read.write规则,因为它们无效.您已经在根目录上授予了完全的读/写访问权限,并且不能在较低级别上取消权限.

I also remove your .read and .write rules on /Users/$uid, since they are ineffective. You're granting full read/write access at the root already, and permission cannot be taken away at a lower level.

这篇关于Firebase警告:使用Firebase Cloud Function搜索数据时,使用未指定的索引的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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