2015-02-11 34 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

相關問題