2015-09-30 21 views
7

所以我有数据,像这样:为什么Firebase“.indexOn”不起作用?

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 
    } 
} 

在我的代码,我需要得到下面一定的“notifyId”的最新通知。下面是代码:

ref.child('pushNotifications') 
    .orderByChild("notifyId") 
    .limitToLast(1) 
    .on("child_added", function (snapshot) { 
     console.log("Found a new notification..."); 
     sendPush(snapshot.val()); 
    }); 

在我火力安全规则,我有这样的指数规则:

"pushNotifications": { 
     ".read": "auth != null", 
     ".write": "auth != null", 
     "$pushId": { 
     ".indexOn": ["notifyId", "timestamp"], 
     } 
    }, 

这是该指数结构遵循由火力地堡文档浏览:docs

但是,当我运行的代码,控制台仍然日志

FIREBASE警告:使用未指定的索引。考虑在/ pushNotifications处添加“.indexOn”:“notifyId”到您的安全规则以获得更好的性能

由于我已经做到了,所以没有任何意义。请帮忙!

+0

顺便说一句,这个相同的主题是不到一天前在这里解决:htt电话号码://stackoverflow.com/questions/32849805/firebase-index-rule-for-attributes-under-key-created-by-childautobyid –

回答

23

你需要把比你做了什么的.indexOn一个水平:

"pushNotifications": { 
    ".read": "auth != null", 
    ".write": "auth != null", 
    ".indexOn": ["notifyId", "timestamp"] 
}, 

为了比较总是看Firebase documentation on indexes,使用这种JSON例如:

"dinosaurs": { 
    "lambeosaurus": { 
    "height" : 2.1, 
    "length" : 12.5, 
    "weight": 5000 
    }, 
    "stegosaurus": { 
    "height" : 4, 
    "length" : 9, 
    "weight" : 2500 
    } 
} 

有了这个索引规则:

{ 
    "rules": { 
    "dinosaurs": { 
     ".indexOn": ["height", "length"] 
    } 
    } 
}