2017-10-13 78 views
0

我試圖用orderBy進行查詢,並且我得到另一個空的子女。Firebase OrderByChild返回額外的空子女

部件.TS

loadCourses(){ 
this.profileProvider.getSpecificCourse().on('value',f=>{ 
    console.log(f.val()); 
}) 

}

提供商

 getSpecificCourse(){ 
    return firebase.database().ref(`CoursesRelated`).orderByChild('cs').equalTo(2); 

} 

火力結構 enter image description here

得到另一空節點 enter image description here

預期結果: 沒有空節點的結果相同。

此外,我想做另一個orderby並得到ls等於6,我該怎麼做?

回答

1

您正在使用數字索引來存儲節點,這意味着Firebase SDK將它們解釋爲數組。對於結果:{ "0": {...}, "2": {...}它然後創建一個三元素陣列[{...}, null, {...}]

有關Firebase如何處理陣列的更好,更長的解釋,請閱讀此博文Best Practices: Arrays in Firebase

擺脫此行爲的方法是不使用順序數字鍵。最簡單的方法是通過調用Firebase的push()方法(以-K...生成密鑰並解釋爲here)或通過在固定字符串前添加數字(例如Course0Course1Course2)來添加項目。由於這些格式是字符串,因此它們會繞過Firebase SDK的數組強制邏輯。