angularfire - Firebase force to use map rather than array -


in firebase quide read array: https://www.firebase.com/docs/web/guide/understanding-data.html#section-arrays-in-firebase

especially :

however, developers storing arrays in firebase database, when data read using val() or via rest api, if data looks array, firebase clients render array. in particular, if of keys integers, , more half of keys between 0 , maximum key in object have non-empty values, firebase clients render array. latter part important keep in mind.

is there way force firebase use map , never convert data array? configuration?

in example in firebase have got structure that:

{ "0": {   "drivers" : {     "12" : {       "latitude" : 50.076574,       "longitude" : 19.9209708,       "timestamp" : 1456311442329     },     "13" : {       "latitude" : 50.0166148,       "longitude" : 20.9863258,       "timestamp" : 1456395866163     }   } } }, { "1" : {   "drivers" : {     "10" : {       "driver_id" : 10,       "latitude" : 50.076574,       "longitude" : 19.9209708,       "timestamp" : 1456311442329     },     "17" : {       "driver_id" : 17,       "latitude" : 50.0166148,       "longitude" : 20.9863258,       "timestamp" : 1456395866163     }   } }  } 

and in java script read json array, because firebase smart , convert data array but, expect map. behavior me unstable.

 [ {       "drivers" : {         "12" : {           "latitude" : 50.076574,           "longitude" : 19.9209708,           "timestamp" : 1456311442329         },         "13" : {           "latitude" : 50.0166148,           "longitude" : 20.9863258,           "timestamp" : 1456395866163         }       }     }, {       "drivers" : {         "10" : {           "driver_id" : 10,           "latitude" : 50.076574,           "longitude" : 19.9209708,           "timestamp" : 1456311442329         },         "17" : {           "driver_id" : 17,           "latitude" : 50.0166148,           "longitude" : 20.9863258,           "timestamp" : 1456395866163         }       }     } ] 

the easiest way never array use strings keys. if natural keys numbers, prefix them string.

e.g.

var words = ['zero', 'one', 'two', 'three']; words.foreach(function(word, index) {   ref.child('word_'+index).set(word); }); 

this way you'll end with:

{   "word_0": "zero",   "word_1": "one",   "word_2": "two",   "word_3": "three" } 

and firebase never coerce array.


Comments

Popular posts from this blog

gridview - Yii2 DataPorivider $totalSum for a column -

java - Suppress Jboss version details from HTTP error response -

Sass watch command compiles .scss files before full sftp upload -