uncaught TypeError: undefined is not a function when using JSON object with name

uncaught TypeError: undefined is not a function when using JSON object with name

我将 D3js 与 Mongodb 和 AnguarlJS 一起使用来显示我的数据。一切都很好,直到我给我的 JSON 数组命名。然后 angular 开始抱怨一些东西,我不确定为什么。

这是原始的 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也是returns数组中的同一个对象

编辑

错误:

未捕获类型错误:undefined 不是函数

console.log(数据) -> 对象 {mymood: 数组[5]}

console.log(data.mymood) -> [对象、对象、对象、对象、对象]

对完整代码感兴趣的人的要点

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

数组在 es5 中有一个内置的 for each,对象没有。

您的代码似乎工作正常(根据我的测试)。我会建议尝试 调试 你的代码,或者如果你觉得更舒服,请尝试尽可能多地 console.log(...) 并继续检查你的 浏览器控制台 从头开始(我无话可说)。也尝试使用 Chrome 中的 Developer Tools,这是一个更好的选择。

我尝试使用 d3.js 库复制您的上下文并上传了一个 json 文件将你的 data 发送到我的 Dropbox 只是为了能够执行 ajax 请求和一切都很好 (新的 JSON 结构也有效)。这是我所做的,可能可以帮助您学习新东西 (见下文)

可能 hints/suggestions 解决您的问题:

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/Whosebug/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: 
     * 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);
});

正如 Oscar 指出的那样,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) { 

我抓取了您的项目存储库并在我的本地环境中试用了这些,我可以在没有看到任何错误的情况下渲染饼图和弹出窗口。