Reputation: 6331
I have been having a bit of difficulty creating and displaying separate node from my Firebase database.
Here is a shot of a child node called "Units"
, as it looks in the Firebase console, under which I wish to have separate single "Unit"
nodes(1,2,3...) each with their own properties.
I don't know what I am doing wrong but when the data returns there is a null
at the beginning of each new child node that causes the data to display incorrectly when iterated over in the view.
Here is a snapshot of the console.log
in the browser as the data comes back:
As you can see there is a null
at the top of each new nested node before the actual data. I am thinking maybe it is something to do with the (1,2,3) nodes not having any values, but if I give them values I can no longer nest data inside each unit.
I am relatively new to Firebase so forgive me if I am making some basic error. Thanks in advance!
Upvotes: 2
Views: 1494
Reputation: 599816
When you use numeric property names, Firebase will see if you're trying to store an array. The legacy documentation on firebase.com had this to say about it:
when data is read using
val()
or via the REST api, if the data looks like an array, the server will render it as an array. In particular, if all of the keys are integers, and more than half of the keys between 0 and the maximum key in the object have non-empty values, then it is treated as an array.
Since your data satisfies these criteria, Firebase returns it as an array. And since array are 0-based, it adds the missing 0th element.
To prevent this, you have a few options:
Don't use numeric keys, but use Firebase push()
to generate your keys
Don't use numeric keys, by prefixing your numbers with a constant string: Unit1
, Unit2
, etc
Start counting from 0 (good suggestion from André Kool)
OK, those are really the only options I can think of.
Upvotes: 6