eric
eric

Reputation: 321

Inconsistantly cannot name a key as an integer in Firebase

I am having some trouble doing depth first searches of some data stored in Firebase, due to some unpredicted behaviour.

I have the following data structure:

"someData":
     |_______>"01":  (take note this is a key:value, not intended to be array index)
     |          |_______"child1"
     |          |_______"child2"
     |_______>"02":
                |_______"child3"
                |_______"child4"

using:

var ref = new Firebase("https://myURL.firebaseio.com/someData");
var sync = $firebase(ref);                                
var someData = sync.$asObject();

One would expect someData to be:

 "someData" [with its children stored as OBJECT]:
     |_______>"01"  [with its children stored as OBJECT]:
     |          |_______"child1"
     |          |_______"child2"
     |_______>"02"  [with its children stored as OBJECT]
                |_______"child3"
                |_______"child4"

However, someData becomes:

 "someData" [with its children stored as ARRAY]:
     |_______>"01"  [with its children stored as OBJECT]:
     |          |_______"child1"
     |          |_______"child2"
     |_______>"02"  [with its children stored as ARRAY]
                |_______"child3"
                |_______"child4"

Is there a way I can force someData to be in the form I desire, apart from changing my naming convention?

If I have a key named '01', the data is interpreted as an array, despite sync.$asObject(). I cannot work out when data will be interpreted as OBJECT, or ARRAY, when it has a "number" (as string) as a KEY

Update: not really an answer, but a workaround

Since this is taken from Firebase blog: when reading a location, if the keys of all objects look like numbers, it assumes it’s an ordered array, possibly sparse, and returns it as an array.

Hence, it is important that the keys do not look like indexes. I have changed my key names to be "m01" from "01", and avoided the inconsistent issue all together.

Upvotes: 0

Views: 395

Answers (1)

eric
eric

Reputation: 321

Not really an answer, but a workaround

Since this is taken from Firebase blog: when reading a location, if the keys of all objects look like numbers, it assumes it’s an ordered array, possibly sparse, and returns it as an array.

Hence, it is important that the keys do not look like indexes. I have changed my key names to be "m01" from "01", and avoided the inconsistent issue all together.

Upvotes: 2

Related Questions