我构建了一个 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/