2015-09-30 10 views
7

Così ho dati in questo modo:Perché questo Firebase ".indexOn" non funziona?

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

Nel mio codice, ho bisogno di ottenere l'ultima notifica di seguito per un certo "NotifyID". Ecco il codice:

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

Nella mia regola di sicurezza Firebase, ho questa regola indice:

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

E 'il seguito struttura di indice dei documenti Firebase qui: docs

Ma quando Eseguo il codice, la console continua a registrare

AVVISO PERICOLO: Utilizzo di un indice non specificato. Considera l'aggiunta di ".indexOn": "notifyId" a/pushNotifications alle tue regole di sicurezza per prestazioni migliori

Che non ha senso poiché l'ho già fatto. Per favore aiuto!

+0

Btw, questo stesso argomento è stato indirizzato meno di un giorno fa qui: htt p: //stackoverflow.com/questions/32849805/firebase-index-rule-for-attributes-under-key-created-by-childautobyid –

risposta

23

è necessario mettere il più alto .indexOn un livello di quello che hai fatto:

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

Per fare un confronto guardare sempre al Firebase documentation on indexes, che utilizza questo esempio JSON:

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

Con questa regola l'indicizzazione :

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