javascript - Firebase 警告 : Using an unspecified index when search data with Firebase Cloud Function

标签 javascript firebase firebase-realtime-database google-cloud-functions

我构建了一个 Firebase 云函数,用于查找“IsNotificationEnabled”的值等于 true 的用户。 我的一部分功能

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 的用户。当我运行它时,我会进入日志

[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 控制台中插入的规则

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

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


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

最佳答案

如消息所述,您需要将索引添加到 /Users。您现在将它添加到低一级,它不起作用。

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

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

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

关于javascript - Firebase 警告 : Using an unspecified index when search data with Firebase Cloud Function,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50464080/

相关文章:

java - 如何从Android Firebase实时数据库中的一个节点检索所有数据?

java - 如何获取此代码的父级 A+ 的名称?

javascript - 界面网页 View 。使用 javascript 替换 css 属性

javascript - 隐式全局 "item"变量 - Internet Explorer 和 FireFox 之间的区别

javascript - Firebase 函数。httpsCallable(...).then 不是函数

android - FirebaseInstanceId.getInstance() 返回 null

javascript - QML 中的可折叠面板

javascript - 如何查看store是什么类型?

swift - 是否可以在 Firebase 中为 child 添加密码?

javascript - AngularFire2实时数据库按日期值排序