Firebase 警告:使用 Firebase Cloud Function 搜索数据时使用未指定的索引



我构建了一个Firebase Cloud Function,用于查找值为"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/数据库: 火力基地警告: 使用未指定的索引。您的数据将被下载和过滤 在客户端上。考虑添加".indexOn":"IsNotificationEnabled" at/用户使用您的安全规则以获得更好的性能。

我在火力控制台中插入的规则

{
"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上运行,因此您需要在该级别定义索引。

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

相关内容

最新更新