简体   繁体   English

记录到控制台会显示已定义的值,但将属性错误视为未定义

[英]Logging to console shows defined value, but using property errors out as undefined

I am trying to debug a TypeError: Cannot read property 'name' of undefined. 我正在尝试调试TypeError:无法读取未定义的属性“name”。

I set up console logging to log the values, and they are defined when I console.log them, but they are undefined when I try to use them to create my Place object below. 我设置控制台日志记录来记录值,它们是在我console.log它们时定义的,但是当我尝试使用它们在下面创建我的Place对象时它们是未定义的。

Is .name special? .name特别吗?

doSearch(term: string) {
this.searchService.onSearchClick(term).subscribe((data) => {
  this.searchResults = [];
  console.log(data); //works
  if (data.hasOwnProperty('candidates')) {
    data['candidates'].forEach( (place) => {
      console.log(place); //works
      console.log(place.formatted_address); //works
      console.log(place.name); //works 
      console.log(place.place_id); //works
      this.searchResults.push(new Place(place.formatted_address,
        '',
        '',
        '',
        new firebase.firestore.GeoPoint(0, 0),
        place.name,
        new firebase.firestore.GeoPoint(0, 0),
        [],
        place.place_id, //errors out here...
        99,
        99,
        new firebase.firestore.GeoPoint(0, 0),
        '',
        '',
        '',
        []));
    });
  }
});

I am expecting that place.name is defined, because the console log is showing it is, but I get an undefined error. 我期望place.name被定义,因为控制台日志显示它,但我得到一个未定义的错误。

The code above logs out: 上面的代码注销:

The object, The array, the arrayobject formatted_address, the arrayobject name, the arrayobject place_id 对象,数组,arrayobject formatted_address,arrayobject名称,arrayobject place_id

All are defined and present when I console.log. 我在console.log中定义并显示所有内容。

But it errors out on the place.name when I am trying to create a Place object. 但是当我尝试创建一个Place对象时,它会在place.name上出错。

Credit to ConnorsFan for putting me in the right direction. 感谢ConnorsFan让我朝着正确的方向前进。

The error was happening later when the html was trying to render a component. 当html尝试渲染组件时,错误发生在以后。 The place object was created successfully. place对象已成功创建。 I just assumed it was erroring out in the wrong location in the code. 我只是假设它在代码中的错误位置出错。 I should really read the line numbers that the console provides to save myself a headache next time... 我应该真正阅读控制台提供的行号,以便下次让自己头疼...

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Chrome控制台将对象属性显示为未定义 - Chrome console shows object property as undefined 控制台日志显示数组的特定值的“未定义” - Console log shows 'undefined' for a specific value of an array Chrome控制台显示未捕获的TypeError:无法读取未定义的属性“开始” - Chrome console shows Uncaught TypeError: Cannot read property 'start' of undefined 为什么我console.log一个对象,它显示对象,但是当我console.log时,它显示未定义 - Why I console.log a Object,it shows object,but when I console Object.value ,it shows undefined 选择显示未定义,但控制台日志显示值angularjs - select showing undefined but console log shows value angularjs 控制台中的对象显示带有值的getter,但在代码中返回未定义 - Object in console shows getter with a value but in the code it returns undefined 异步函数返回值,显示在控制台上,但在其他地方未定义 - Async function returns value, shows up on console, but undefined elsewhere JavaScript obj[property] 抛出“属性未定义”而不是给出未定义的值 - JavaScript obj[property] throwing “property not defined ” instead of giving undefined value 当我控制台输出 userId 时,useParams() 没有获取 userId 并在控制台中显示 undefined - useParams() not getting the userId and shows undefined in console when i console out userId Console.logging 对象显示该对象包含尚未分配的属性 - Console.logging an object shows that the object contains a property that had not yet been assigned
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM