Reputation: 357
I'm trying to make query with orderBy and I get another empty child.
component .ts
loadCourses(){
this.profileProvider.getSpecificCourse().on('value',f=>{
console.log(f.val());
})
}
provider
getSpecificCourse(){
return firebase.database().ref(`CoursesRelated`).orderByChild('cs').equalTo(2);
}
getting the another empty node
expected result: the same result without the empty node.
in addition I want to make another orderby and get the ls equal to 6, how can I do that?
Upvotes: 0
Views: 931
Reputation: 598740
You're using numeric indexes to store the nodes, which means that Firebase SDK interprets them as an array. For the results: { "0": {...}, "2": {...}
it then creates a three element array [{...}, null, {...}]
.
For a good, longer explanation of how Firebase deals with arrays, read this blog post Best Practices: Arrays in Firebase.
The way to get rid of this behavior is to not use sequential numeric keys. The simplest way to do this is to either add the items by calling Firebase's push()
method (which generates keys in a format -K...
and is explained here), or by prefixing the numbers with a fixed string (e.g. Course0
, Course1
, Course2
). Since these format are strings, they bypass the Firebase SDK's array-coercion logic.
Upvotes: 1