2015-02-11 76 views
2

我正在使用D3js与Mongodb和AnguarlJS来显示我的数据。所有这一切都很好,直到我给我的JSON数组命名为止。然后角开始抱怨的东西,我不知道为什么。未捕获TypeError:undefined不是使用名称为JSON对象时的函数

这是原来的JSON数组,这个原代码工作

[ 

    { 
    "defaultCategory":"Happy", 
    "timesUsed":2704659 
    }, 
    { 
    "defaultCategory":"Sad", 
    "timesUsed":4499890 
    }, 
    { 
    "defaultCategory":"Laughter", 
    "timesUsed":2159981 
    }, 
    { 
    "defaultCategory":"Smirk", 
    "timesUsed":3853788 
    }, 
    { 
    "defaultCategory":"Flirty", 
    "timesUsed":14106543 
    } 
] 


d3.json("data.json", function(error, data) { 
     data.forEach(function(d) { 
      d.timesUsed =+ d.timesUsed; 
    }); 

但是当我的JSON改变这种格式,它打破

{ 
    "mymood":[ 

    { 
    "defaultCategory":"Happy", 
    "timesUsed":2704659 
    }, 
    { 
    "defaultCategory":"Sad", 
    "timesUsed":4499890 
    }, 
    { 
    "defaultCategory":"Laughter", 
    "timesUsed":2159981 
    }, 
    { 
    "defaultCategory":"Smirk", 
    "timesUsed":3853788 
    }, 
    { 
    "defaultCategory":"Flirty", 
    "timesUsed":14106543 
    } 
] 

} 


d3.json("data.json", function(error, data) { 
     data.mymood.forEach(function(d) { 
      d.timesUsed =+ d.timesUsed; 
    }); 

在Chrome控制台中的错误发生在线data.mymood.foreach线, ,但我不明白为什么,因为它完全返回相同的JSON,就好像没有这样的名字

[对象,对象,对象,对象,对象]

和在该函数的参数d也返回数组

编辑

误差内相同的对象:

遗漏的类型错误:undefined不是函数

console.log(data) - > Object {mymood:Array [5]}

的console.log(data.mymood) - > [对象,对象,对象,目标,对象]

要点对于那些有兴趣谁在全码

https://gist.github.com/gwong89/e3a29b64d94ad20256bb

+1

当你'console.log(data)'或'console.log(data.mymood)'什么出来? – 2015-02-11 19:33:54

+0

你得到的错误是什么? – 2015-02-11 19:34:06

+0

检查编辑上面,我刚刚添加 – 2015-02-11 19:40:16

回答

1

像奥斯卡曾指出,foreach循环工作正常。但是,经过仔细看看你的要点,如果你尝试使用mymood键,请确保您还更改行55你在哪里使用

var g = svg.selectAll('g') 
      .data(pie(data)) 

var g = svg.selectAll('g') 
      .data(pie(data.mymood)) 

而且上线76,你有

var total = d3.sum(data.map(function(d) { 

var total = d3.sum(data.mymood.map(function(d) { 

我抓住了您的项目回购,并在我的本地环境中尝试了这些,我可以呈现饼图并弹出而不会看到任何错误。

+0

非常感谢!我没有意识到错误来自那里,铬控制台不断抱怨这是实际的d3.js文件 – 2015-02-14 05:40:20

-3

阵列具有内置的对于es5中的每一个,对象都没有。

+1

data.mymood返回一个数组,而不是一个对象。 – 2015-02-11 19:34:23

0

你的代码似乎工作正常(从我测试过的)。我会建议尝试调试你的代码,或者如果你感觉更舒服尝试做多console.log(...)尽可能并保持从一开始(我无话可说)检查您浏览器控制台。尝试还使用开发者工具Chrome这是一个更好的选择。

我试着用d3.js库来复制你的背景和上传json文件与数据的Dropbox只是为了能够执行AJAX要求,一切都很好再次(也是新的JSON结构有效)。以下是我所做的,可能可以帮助你学习新的东西(见下文)

可能的提示/建议,以解决您的问题:

According to the question title, it seems that data.mymood is undefined so you are not able to do a forEach of something that doesn't exist. Try validating things and avoid null pointers (see example).

JSON structures are valid so that's not the problem.

Syntax seems to be valid from what I've tested.

Check if there's a conflict between libraries, try to do some research about using all the libraries needed together (not sure if this is your situation but could happen).

Check your browser console and do some debugging as the first paragraph says.

Check if your request is not timing out or something else is happening, check the Network tab or log your requests in browser console using Developer Tools from Chrome(just saying).

现场演示:http://jsfiddle.net/29f6n8L7/

d3.json('https://dl.dropboxusercontent.com/u/15208254/stackoverflow/data.json', function(data) { 

    // Some ways to avoid null pointers 
    var obj = (data || {}), // If 'data' is null, initialize as an Object 
     moods = (obj.mymood || []), // If 'obj.mymood' is null, initialize as an Array 
     time = 0, 
     mood; 

    // Same way to get 'mymood' from the object 
    console.log('Your obj.mymood contains: %o', obj.mymood); 
    console.log('Your obj["mymood"] contains: %o', obj['mymood']); 

    /** 
    * I will suggest to use a traditional loop instead of 'forEach'. 
    * Please read: http://stackoverflow.com/a/9329476/1178686 
    * for a better explanation  
    */ 
    for (var i = 0; i < moods.length; ++i) { 
     mood = moods[i]; 
     if (mood) { 
      time += mood.timesUsed; 
     } 
    } 

    // Output 
    console.log('Sum of timesUsed is: %s', time); 
}); 
+0

嘿奥斯卡,伟大的职位,我很感激。我摆弄着你的建议,for循环可以工作,但是重命名我所有的变量并破解某些工作将会带来很大的痛苦。另外,我再次检查了Chrome控制台,并意识到来自d3.js库本身。 – 2015-02-12 08:46:47

相关问题