我构建了一个 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;
})
})
..........................
正如您在此处看到的,我正在查找“通知已启用”= true 的用户。当我运行它时,我会进入日志
[2018-05-22T09:12:57.352Z] @firebase/database:FIREBASE 警告:
使用未指定的索引。您的数据将被下载并过滤
在客户端上。考虑添加 ".indexOn": "IsNotificationEnabled"
/用户遵守您的安全规则以获得更好的性能。
我在 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
,您需要在该级别定义索引。
我也删除你的.read
and .write
规则/Users/$uid
,因为它们无效。您已经在根授予完全读/写访问权限,并且不能在较低级别剥夺权限。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)