I am parsing a smil (xml) file to get a list of pathname. 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.onplete (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.
I am parsing a smil (xml) file to get a list of pathname. 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.onplete (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.
Share Improve this question edited Jul 26, 2013 at 15:57 dda 6,2132 gold badges27 silver badges35 bronze badges asked Jul 26, 2013 at 15:50 DrakaSANDrakaSAN 7,9088 gold badges57 silver badges96 bronze badges 5-
1
Both the variable
i
andsmil.playlist.video
areundefined
. Putvar i
closer to the while-loop. (in the parse callback) – Halcyon Commented Jul 26, 2013 at 15:51 - How is that i is undefined? Isn t it declared in the same time as smil? – DrakaSAN Commented Jul 26, 2013 at 15:57
-
1
I don't know,
i
is defined globally so who knows where it's been. It'sundefined
when this callback is ran. – Halcyon Commented Jul 26, 2013 at 15:59 - Good point, it indeed caused another error after the one that made me post – DrakaSAN Commented Jul 26, 2013 at 16:03
- @dda Thanks for fixing the typo – DrakaSAN Commented Jul 26, 2013 at 16:03
3 Answers
Reset to default 2playlist
is an array.
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:
playlist:
[ <-- Array declaration
{ ... }
]
However, you're doing:
smil.playlist.video[i].src
-------------^
You'll need to refer to an index of playlist
.
It's saying that video[i] is undefined and hence any method on an undefined object will be undefined! Try printing out smil.playlist.