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