DrakaSAN
DrakaSAN

Reputation: 7863

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. 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.

Upvotes: 0

Views: 8146

Answers (3)

Pablo208
Pablo208

Reputation: 23

It's saying that video[i] is undefined and hence any method on an undefined object will be undefined! Try printing out smil.playlist.

Upvotes: 1

Zbigniew
Zbigniew

Reputation: 27614

playlist is an array.

Replace

smil.playlist.video[i].src

with

var index = 0; // or whatever
smil.playlist[index].video[i].src

Upvotes: 2

Mike Christensen
Mike Christensen

Reputation: 91686

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.

Upvotes: 2

Related Questions