[英]Cannot read property 'undefined' of undefined, but I'm sure the var is valid
I am parsing a smil (xml) file to get a list of pathname. 我正在解析一个smil(xml)文件以获取路径名列表。 The parsing go well, and is stocked in a huge literal object. 解析进行得很好,并存储在一个巨大的文字对象中。
But when I try to get the information back, I only get: 但是,当我尝试取回信息时,只会得到:
/home/pi/RaspberryPiTV-master/app.js:158
Playlist.push(smil.playlist.video[i].src);
^
TypeError: Cannot read property 'undefined' of undefined
at /home/pi/RaspberryPiTV-master/app.js:158:46
at /home/pi/RaspberryPiTV-master/app.js:321:39
at Object.oncomplete (fs.js:93:15)
The code is as follows: 代码如下:
var smil={}, i=0;
Playlist=[];
parse("/home/pi/test.smil", function (parsed){
smil=parsed;
console.dir(util.inspect(smil, false, null));
do
{
Playlist.push(smil.playlist.video[i].src);
i=i+1;
}while(i<smil.playlist.video.length-1);
...
}
The function parse (pathname, callback) is quite huge, but does work since the print of it does work: 函数解析(路径名,回调)非常庞大,但是可以正常工作,因为它的打印确实可以工作:
{
stream:
[
{ name: 'Stream1' }
],
playlist:
[
{ video:
[
{ src: 'L.mp4', start: '5', length: '5' },
{ src: 'SW.mp4', start: '50', length: '5' },
{ src: 'HN.mp4', start: '150', length: '5' }
],
name: 'pl1',
playOnStream: 'Stream1',
repeat: 'true',
scheduled: '2013-07-23 11:00:00'
}
]
}
Am I missing something? 我想念什么吗? I just don't understand why I get undefined since I do get the print correctly. 我只是不明白为什么我会得到不确定的定义,因为我可以正确打印。
playlist
is an array. playlist
是一个数组。
Replace 更换
smil.playlist.video[i].src
with 同
var index = 0; // or whatever
smil.playlist[index].video[i].src
According to your JSON, playlist
is an array: 根据您的JSON, playlist
是一个数组:
playlist:
[ <-- Array declaration
{ ... }
]
However, you're doing: 但是,您正在执行以下操作:
smil.playlist.video[i].src
-------------^
You'll need to refer to an index of playlist
. 您需要引用playlist
的索引 。
It's saying that video[i] is undefined and hence any method on an undefined object will be undefined! 就是说video [i]是未定义的,因此未定义对象上的任何方法都将是未定义的! Try printing out smil.playlist. 尝试打印出smil.playlist。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.