2015-09-30 150 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"] 
    } 
    } 
}