Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why do firebase collections seem to begin with a null row?

firebase example database

I've created a simple firebase data set to test some REST calls with (see the image below). I'm wondering why, when I query the collections in the database, firebase always return a null row first and then the actual rows. Here is what I get when I export the data. Notice the null rows under systems and system_types:

{
  "systems" : [ null, {
    "system_type_id" : 2,
    "name" : "Commodore 128",
    "id" : 1
  }, {
    "system_type_id" : 1,
    "name" : "Difference Engine",
    "id" : 2
  }, {
    "system_type_id" : 2,
    "name" : "Osborne",
    "id" : 3
  } ],
  "system_types" : [ null, {
    "name" : "Babbage",
    "id" : 1
  }, {
    "name" : "Von Neumann",
    "id" : 2
  } ]
}
like image 922
cayblood Avatar asked Mar 20 '13 21:03

cayblood


People also ask

Is Firebase SQL or NoSQL?

The Firebase Realtime Database is a cloud-hosted NoSQL database that lets you store and sync data between your users in realtime.

How do I know if my DataSnapshot is empty?

You can use hasChildren() to determine if a DataSnapshot has any children. If it does, you can enumerate them using forEach() . If it doesn't, then either this snapshot contains a primitive value (which can be retrieved with val() ) or it is empty (in which case, val() will return null ).

What is the difference between firestore and Firebase?

Cloud Firestore is Firebase's newest database for mobile app development. It builds on the successes of the Realtime Database with a new, more intuitive data model. Cloud Firestore also features richer, faster queries and scales further than the Realtime Database. Realtime Database is Firebase's original database.

What is collection in Firebase?

A collection contains documents and nothing else. It can't directly contain raw fields with values, and it can't contain other collections. (See Hierarchical Data for an explanation of how to structure more complex data in Cloud Firestore.) The names of documents within a collection are unique.


1 Answers

Firebase automatically detects when an object 'looks like' an array, and converts it accordingly. Since 'systems' and 'system_types' have children of 1, 2, and 3, Firebase is automatically converting these to arrays at output time. However, since you didn't specify a value for index 0, Firebase is just inserting an empty element.

I'd suggest either using zero-based ID's, or perhaps starting your id's with something non-numeric so we know it's not an array (ie: item1, item2, item3).

You could also just ignore the null element -- In Firebase null and non-existent are the same thing.

like image 68
Andrew Lee Avatar answered Oct 09 '22 14:10

Andrew Lee