【问题标题】:Error while reading json Uncaught TypeError: Cannot read property 'output' of undefined读取 json 时出错 Uncaught TypeError:无法读取未定义的属性“输出”
【发布时间】:2015-10-19 12:04:19
【问题描述】:

我正在尝试使用 PUBNUB javascript SDK 使用 c3.js 创建一个图表。问题是,当我尝试创建图表时,它不会读取 json 数据我收到错误 Uncaught TypeError: Cannot read property 'output' of undefined.

完整的数据是(我通过python发送数据)

m = {
    "devices": [
        {
            "Name": "bdev0",
            "output": {
                "IO_Operations": 0,
                "Bytes_Read": 0,
                "Bytes_Written": 0
            }
        },
        {
            "Name": "bdev1",
            "output": {
                "IO_Operations": 0,
                "Bytes_Read": 0,
                "Bytes_Written": 0
            }
        }
    ]
}

而 JavaScript 是

eon.chart({
    pubnub   : pubnub,
    history  : false,
    channel  : 'chanel',
    flow     : true,
    generate : {
        bindto : '#chart_1',
        size: {
        height: 180,
        width: 500
    },
        data   : {
            x      : 'x',
            labels : true
        },
        axis : {
            x : {
                type : 'timeseries',
                tick : {
                    format : '%H:%M:%S'
                },
                zoom: {
                   enabled: true
                }
            }
        }
    },

    transform : function(m) {
        return { columns : [
            ['x', new Date().getTime()],
            ['Bytes Written', m.devices[i].output.Bytes_Read],
            ['Bytes Read', m.devices[i].output.Bytes_Written]


        ] };
    }
});

【问题讨论】:

    标签: javascript json d3.js pubnub c3.js


    【解决方案1】:

    您正在使用devices[i],但您没有定义i 是什么。如果要将其用作数组索引,则需要给 i 一个值。

    【讨论】:

    • 使用 [i] 意味着它遍历数组中的所有元素,当我尝试在简单的 javascript 中执行此操作时,它可以工作,为什么会这样?例如看到这个 jsfiddle jsfiddle.net/umLwwq2a
    • @Imo 正如我所说,您需要定义i。您在小提琴中的代码正是这样做的,所以它可以工作。您在上面发布的代码没有定义i,所以当然它不起作用。尝试从您的 JSFiddle 中取出 for (var i 部分,看看它的效果如何。
    猜你喜欢
    • 2021-04-28
    • 2017-07-20
    • 1970-01-01
    • 2020-03-28
    • 2023-04-11
    • 2016-04-22
    • 2015-06-08
    • 2014-03-17
    • 1970-01-01
    相关资源
    最近更新 更多