2016-09-13 72 views
-1

我解析CSV文件到JSON然后添加我自己的标头为其他奇怪的原因。所以我有一个看起来像这样的数据:在控制台中,我可以使用JSON中的项目,通过console.log他们不显示

enter image description here

在我的js文件我有

的console.log(jsonData.theData [0] .symbol);

,我也得到:

admin.js:46 Uncaught TypeError: Cannot read property '0' of undefined 

看起来0是指给我,但是当我只需在浏览器中的js文件我取得好成绩安慰相同的命令。

enter image description here 我试图

JSON.stringify(jsonData.theData [0])

具有相同的误差引起。

任何想法,为什么这不工作?

JS文件如下:

var jsonData = {}; 
var theData = []; 

document.getElementById("fileToRead").addEventListener("change",function() { 
    var input = document.getElementById("fileToRead"); 

     for(var i = 0; i < input.files.length; i++){ 
      var files = input.files[i]; 
      Papa.parse(files, { 
      header:false, 
      dynamictyping:true, 
      complete:function(results){ 
       var input = results.data; 
       input.forEach(function(input){ 
        jsonData.theData = theData; 

        var singleEntry = { 
         "symbol" : input[0], 
         "date"  : input[1], 
         "open"  : input[2], 
         "high"  : input[3], 
         "low"  : input[4], 
         "close"  : input[5], 
         "volume" : input[6] 
         } 

        jsonData.theData.push(singleEntry); 
       }) 
       //console.log (jsonData); 
       // document.getElementById("editor").innerHTML = JSON.stringify(jsonData); 
       } // End Complete - Callback 
      }); // End PapaParse 
    } // End for loop 
console.log(jsonData); 
}); 

编辑.................

我加了一些console.logs它并收到这个.. ..

enter image description here

+0

在admin.js文件在哪里你到底叫'的console.log()'函数?我怀疑你在没有定义特定变量的地方调用函数。 – d3r1ck

+0

我将用代码编辑。 – illcrx

+0

我说过的话有帮助吗?我的意思是说它有意义或帮助你? – d3r1ck

回答

1

点击小图标i。它的竞争条件,你的对象没有得到及时加载你的console.log(jsonData.theData[0].symbol);。督察事后加载它为你

+0

我看到小图标,那么如何在显示所有内容之后显示它,而不是之前? – illcrx

0

当你打电话给你

的console.log(jsonData.theData [0] .symbol);

你确定你的jsonData被加载?

编辑(JS代码更新后)

在您的JS代码,没有console.log(jsonData.theData[0].symbol); ......我们不能看到并了解自己的错误。

反正 正如我说的,哪怕你console.log(jsonData);在循环的结束时提出,这可能是因为您加载数据异步

source

由于文件是空的呢解析是异步的,不要忘记回调。

所以你jsonData可能是空的(所以,没有加载)

相关问题