【发布时间】:2021-12-31 00:17:25
【问题描述】:
我构建了一个 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/数据库:FIREBASE 警告: 使用未指定的索引。您的数据将被下载和过滤 在客户端。考虑添加 ".indexOn": "IsNotificationEnabled" 在 /用户遵守您的安全规则以获得更好的性能。
我在 firebase 控制台中插入的规则
{
"rules": {
".indexOn": ["IsNotificationEnabled"],
"Users":{
"$uid":{
".indexOn": ["IsNotificationEnabled"],
".write": "$uid === auth.uid",
".read": "$uid === auth.uid"
}
},
".read": true,
".write": true
}
}
【问题讨论】:
-
我在你的问题中遗漏了这个问题。但是 indexon 不需要更高一级吗?在用户下方而不是 $uid。
-
我的问题,为什么我会收到这个错误以及如何解决它。我编辑了我的帖子并添加了用户结构的图像
-
您是否尝试将 indexon 放在 users 下方而不是 $uid 下方?
-
是的,现在可以使用了。谢谢你,先生。
标签: javascript firebase firebase-realtime-database google-cloud-functions