Firebase不会保存具有空值的键 [英] Firebase won't save keys with null values

查看:103
本文介绍了Firebase不会保存具有空值的键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个像这样的对象:

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
}]}

我使用Firebase保存此对象为:

I save this object using Firebase as:

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

但是,当我在检索相同内容时c>方法并获取值为:

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

snapshot.val()

我得到了对象,但是 null 的值被删除了,即我只有:

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
}]}

由于我使用的是 Fabric.js ,它需要这些值。
请帮助!

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

编辑/更新(黑客)

目前,我正在使用一个奇怪的HACK,然后将对象存储到Firebase我将所有 null 值转换为 0 。但我想知道一个更好的方法。

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);


推荐答案

目前我正在使用一个奇怪的HACK ,在将对象存储到Firebase之前,我将所有空值转换为0.但我想知道更好的方法,请!

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);

这篇关于Firebase不会保存具有空值的键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆