为什么这个Firebase“.indexOn”不行? [英] Why does this Firebase ".indexOn" not work?
问题描述
pushNotifications {
-K - RwEgd541PIGNOXXUH:{
消息:Xiaoyu Hugh Hou出价900美元对你的任务:新P ...
notifyId:facebook:10100683829966199
relatedTask:-Jzl8XKaxGCv19nIOChm
时间戳:1443594566599
$ {
message:Sam Feldt出价1100美元在你的任务上:New Post g ...
notifyId:google:1043268349966197
relatedTask: - Jzl8XKaxGCv19nIOChm
timestamp:1443594721963
} - K - RwuM3 - 0 G0q9I96WHg:{
message:Sam Feldt在您的任务中出价600美元:NO Firebase ...
notifyId :facebook:10100683829966199
relatedTask:-JzTSk2TIlO46oVqJ1Nn
timestamp:1443594741347
}
}
在我的代码中,我需要将最新的通知传递给某个notifyId。这里是代码:
$ $ p $ $ $ $ c $ ref $($'$' $ b .limitToLast(1)
.on(child_added,function(snapshot){
console.log(Found a new notification ...);
sendPush(snapshot。 val());
});
在我的firebase安全规则中,我有这个索引规则:
pushNotifications:{
.read:auth!= null,
.write:auth!= null ,
$ pushId:{
.indexOn:[notifyId,timestamp],
}
},
FIREBASE警告:使用未指定的索引。考虑在/ pushNotifications处添加.indexOn:notifyId到你的安全规则以获得更好的性能
这是没有任何意义的,我已经做到了。请帮忙!
您需要将 .indexOn
你做了:
$ b $ pre $ pushNotifications:{
.read:auth!= null,
.write:auth!= null,
.indexOn:[notifyId,timestamp]
},
作为比较,请查看
, dinosaurs:{$ rel =noreferrer> Firebase关于索引的文档
lambeosaurus:{
height:2.1,
length:12.5,
weight:5000
},
stegosaurus :{
height:4,
length:9,
weight:2500
}
}
$ c
使用此索引规则:
{
rules:{
dinosaurs:{
.indexOn:[height,length]
}
}
}
So I have data like so:
pushNotifications {
-K - RwEgd541PIGNOXXUH: {
message: "Xiaoyu Hugh Hou bid at $900 on your task: New P..."
notifyId: "facebook:10100683829966199"
relatedTask: "-Jzl8XKaxGCv19nIOChm"
timestamp: 1443594566599
} - K - RwpcBlQa04VJT4Bwm: {
message: "Sam Feldt bid at $1100 on your task: New Post g..."
notifyId: "google:1043268349966197"
relatedTask: "-Jzl8XKaxGCv19nIOChm"
timestamp: 1443594721963
} - K - RwuM3 - 0 G0q9I96WHg: {
message: "Sam Feldt bid at $600 on your task: NO Firebase..."
notifyId: "facebook:10100683829966199"
relatedTask: "-JzTSk2TIlO46oVqJ1Nn"
timestamp: 1443594741347
}
}
In my code, I need to get the latest notification below to a certain "notifyId". Here is the code:
ref.child('pushNotifications')
.orderByChild("notifyId")
.limitToLast(1)
.on("child_added", function (snapshot) {
console.log("Found a new notification...");
sendPush(snapshot.val());
});
In my firebase security rule, I have this index rule:
"pushNotifications": {
".read": "auth != null",
".write": "auth != null",
"$pushId": {
".indexOn": ["notifyId", "timestamp"],
}
},
It is the index structure follow by the Firebase docs here: docs
But when I run the code, the console still log
FIREBASE WARNING: Using an unspecified index. Consider adding ".indexOn": "notifyId" at /pushNotifications to your security rules for better performance
Which does not make any sense since I did that already. Please help!
You need to put the .indexOn
one level higher than what you did:
"pushNotifications": {
".read": "auth != null",
".write": "auth != null",
".indexOn": ["notifyId", "timestamp"]
},
For comparison always look at the Firebase documentation on indexes, which uses this JSON example:
"dinosaurs": {
"lambeosaurus": {
"height" : 2.1,
"length" : 12.5,
"weight": 5000
},
"stegosaurus": {
"height" : 4,
"length" : 9,
"weight" : 2500
}
}
With this indexing rule:
{
"rules": {
"dinosaurs": {
".indexOn": ["height", "length"]
}
}
}
这篇关于为什么这个Firebase“.indexOn”不行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!