2015-10-19 80 views
-1

我想用c3.js使用PUBNUB javascript SDK创建图表。问题是,当我尝试创建图表时,它不会读取json数据,我得到错误Uncaught TypeError:无法读取未定义的属性“输出”。阅读json时出错Uncaught TypeError:无法读取未定义的属性'输出'

完整的数据是(我派通过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] 


     ] }; 
    } 
}); 

回答

3

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

+0

使用[i]意味着它会遍历数组中的所有元素,当我尝试在简单的JavaScript中使用它时,它为什么会这样呢?例如看到这个jsfiddle https://jsfiddle.net/umLwwq2a/ – Imo

+1

@Imo正如我所说,你需要**定义'我'**。你在小提琴中的代码完全是这样,所以它的工作原理。你上面发布的代码**没有定义'我'**,所以*当然*它不起作用。尝试从JSFiddle中取出(var i'部分),看看它的工作情况。 – meagar

相关问题