Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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,"stroke":"#3f7cc4", 
    "strokeWidth":12,"strokeDashArray":null
}]}

I save this object using Firebase as:

var myDataRef = new Firebase(<...>);
myDataRef.child("saved_projects").child(authData.uid).update({'P3': _json});

But, when I retrieve the same using Firebase on method and get the value as:

snapshot.val()

I get the object but keys with null values got removed i.e. I only got:

{"objects":[ {"type":"path", "originX":"center", 
 "originY":"center","left":48.59, "top":132.5,"width":64.5, 
 "height":173, "stroke":"#3f7cc4","strokeWidth":12
}]}

This is causing me some weird issues since I'm using Fabric.js and it needs these values. Please help!

EDIT / UPDATE(Hack)

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 a nicer way to do.

function recursivelyReplaceNullToZero(j) {
    for (var i in j){
        if (typeof j[i] === "object") {
            recursivelyReplaceNullToZero(j[i]);
        }
        if (j[i] === null) {
            j[i] = 0;
        }
    }
}
recursivelyReplaceNullToZero(_json);
like image 724
softvar Avatar asked Dec 20 '14 13:12

softvar


People also ask

Does Firebase accept null values?

A null value, on the other hand, indicates that the current user is not signed in using any of the Firebase authentication providers. As declared, these rules dictate that only authenticated app users are allowed read and write access to the project's realtime database data.

Why my data is not stored in Firebase?

Nothing is getting stored, because you are not storing anything, you need to use setValue() to store data. The child() gets reference to a location in the database.

Is Firebase a key-value store?

Firebase is categorized as a NoSQL database program, which stores data in JSON-like documents. In Firebase, a document is a set of key-value pairs defined by a schema.


1 Answers

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!

function recursivelyReplaceNullToZero(j) {
    for (var i in j){
        if (typeof j[i] === "object") {
            recursivelyReplaceNullToZero(j[i]);
        }
        if (j[i] === null) {
            j[i] = 0;
        }
    }
}
recursivelyReplaceNullToZero(_json);
like image 85
softvar Avatar answered Sep 28 '22 01:09

softvar