简体   繁体   English

无法读取未定义的属性“未定义”,但我确定var有效

[英]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.

相关问题 错误:未处理的拒绝(类型错误):无法读取未定义的属性“地图”不确定我错过了什么 - Error: Unhandled Rejection (TypeError): Cannot read property 'map' of undefined not sure what I'm missing 获取 TypeError: Cannot read property 'localeCompare' of undefined for my sortBy function 在我的 React 组件中,我不知道为什么 - Getting TypeError: Cannot read property 'localeCompare' of undefined for my sortBy function in my React component and I'm not sure why 我在下拉菜单中收到“无法读取未定义的属性‘办公室’”错误 - I'm receiving "Cannot read property 'offices' of undefined" error for a dropdown 反应我因为“无法读取未定义的属性&#39;地图&#39;”而发霉 - REACT i'm malding because of "Cannot read property 'map' of undefined" javascript prototype var未定义,但我不确定为什么。 - javascript prototype var is undefined, but I'm not sure why. map 未定义,我不确定它是属性还是我的 api 设置 - map is undefined and I'm not sure if it's the property or my api setup 无法读取未定义的属性“未定义” - Cannot read property 'undefined' of undefined 在 Firebase 中,我收到 Uncaught TypeError: Cannot read property &#39;initializeApp&#39; of undefined,但不确定为什么未定义“firebase”? - In Firebase I am getting an Uncaught TypeError: Cannot read property 'initializeApp' of undefined, but not sure why 'firebase' is not defined? 定义 var 后“未捕获的类型错误:无法读取未定义的属性‘长度’” - "Uncaught TypeError: Cannot read property 'length' of undefined" after defining var 循环中的“ I”之前缺少var或未捕获的TypeError:无法读取未定义的属性“ length” - Missed var before “I” in cycle or Uncaught TypeError: Cannot read property 'length' of undefined
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM