最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Cannot read property 'undefined' of undefined, but I'm sure the var is valid - Stack Overfl

programmeradmin4浏览0评论

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 and smil.playlist.video are undefined. Put var 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's undefined 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
Add a ment  | 

3 Answers 3

Reset to default 2

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

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论