Skip to content Skip to sidebar Skip to footer

Firebase Won't Save Keys With Null Values

I have an object like: var _json = { 'objects':[{ 'type':'path', 'originX':'center', 'originY':'center', 'left':48.59, 'top':132.5, 'width':64.5,'height':173, 'fill':null,

Solution 1:

For the time being I'm using a weird HACK, before storing the object to Firebase I'm converting all the null values to 0. But I want to know much nicer way, please!

functionrecursivelyReplaceNullToZero(j) {
    for (var i in j){
        if (typeof j[i] === "object") {
            recursivelyReplaceNullToZero(j[i]);
        }
        if (j[i] === null) {
            j[i] = 0;
        }
    }
}
recursivelyReplaceNullToZero(_json);

Solution 2:

fill would be a type string and those can be empty in Firestore. strokeDashArray would be a type array and those can contain empty strings as well (among other data types). See image below which is a screenshot of Firestore. You are correct about null though, it won't store properties with value null.

Remember though that Firestore is a document database, and is schema-less - meaning that not every document is required to have all fields (semi-structured).

So, instead of storing value-less properties, simply handle the possibility that the property may not be there in your code, e.g.,

if(doc.exists){
  if(doc.data().fill){
    // now you do what you want with the fill property
  } else {
    // there is no fill data
  }
}

enter image description here

Solution 3:

This is expected behavior. AFAIK json systems work like this.

You should instead in your code set nulls for the keys that are not present in the database. As an analogy: think of the leading zeroes - you DON'T write in front of a number.

Post a Comment for "Firebase Won't Save Keys With Null Values"