【问题标题】:Getting "Cannot read property 'of' of undefined" using angular 8, and httpclient使用 Angular 8 和 httpclient 获取“无法读取未定义的属性”
【发布时间】:2019-09-19 14:45:01
【问题描述】:

我使用以下服务为我的项目配置了新的httpclient。 AppModule.ts 设置正确

getAllLocosByID(locoID: string) {
    return this.http.get<Search[]>(`${environment.apiUrl.searchbylocoid}/${locoID}`);
}

搜索也设置为

export class Search {
    input: string;
    warningMessages: String[];
    errorMessages: String[];
    numFound: number;
    results: String[];
    APP_CODE: string;
}

在我的组件上

logoSearchResults: Search[];
constructor(private data: DataAccessService) { }    
getLocmotives() {
    this.data.getAllLocosByID('A1').subscribe((res: Search[]) => {
    // console.log(Array.of(res));
    // console.log(typeof (response));
    // this.logoSearchResults = res;
    this.logoSearchResults = { ...res };
    console.log(this.logoSearchResults);
 },
  error => {
    console.log("error");
  })
}

这让我在浏览器控制台上关注。基于 httpclient 文档,返回响应已经是一个可观察的 json,我将其形成为 Search[]

{input: "A1", warningMessages: Array(0), errorMessages: Array(0), numFound: 241, results: Array(3), …}

到目前为止,一切都很好!但是让我们把它放在 HTML 部分,并展示给最终用户

我在 HTML 上放置了一个与 click 配合使用的按钮

<button class="btn btn-primary" (click)="getLocmotives()">
   Give me answer
</button>
{{ logoSearchResults }}

这是我从{{ logoSearchResults }}屏幕上看到的

[object object]

通过更新 HTML 代码来循环遍历这个数组

<ul class="list-group">
  {{ logoSearchResults }}
  <li class="list-group-item" *ngFor="let result of logoSearchResults">
     {{ result }}
  </li>
</ul>

我收到以下错误

ERROR Error: Cannot find a differ supporting object '[object Object]' of type 'object'. NgFor only supports binding to Iterables such as Arrays.

通过将 HTML 代码更新为

<ul class="list-group">
 {{ logoSearchResults }}
 <li class="list-group-item" *ngFor="let result of Array.of(logoSearchResults)">
    {{ result }}
 </li>

我收到以下错误

ERROR TypeError: Cannot read property 'of' of undefined

我应该在 HTML 或 Angular 部分中做什么才能在屏幕上显示我的数据?

【问题讨论】:

  • 在模板中你说...of Array.of(...,但总是隐含this。因此它被解析为this.Array.of(logoSearchResults),并且由于您在随附的TS范围内没有this.Array - 未定义。

标签: angular http httpclient angular-httpclient


【解决方案1】:

你应该给 ngfor 添加一个数组: *ngFor="let result of logoSearchResults.results"

【讨论】:

  • 我在屏幕上得到结果。到目前为止,这个问题的最准确答案,但在单击Give me answer 之前,我仍然在控制台上收到此错误。无法读取未定义的属性“结果”。你有解决办法吗?
  • 是的,因为它没有被初始化,所以使用这个:*ngFor="let result of logoSearchResults?.results"
【解决方案2】:

试试

<pre>{{ logoSearchResults | json}}</pre>

json 管道 将显示您的确切 json。

也会对齐 json,使其可读。

【讨论】:

  • 有趣的是 json 管道有助于显示屏幕,感谢您的回答
【解决方案3】:

这就是问题所在 this.logoSearchResults = { ...res }; 您将 res 放入对象中。您不能在对象上循环使用 of。我看到你服务了

this.http.get<Search[]>

应该返回一个搜索数组,所以你应该有

this.logoSearchResult=res;

在你的循环中更改为

{{ result|json }}

【讨论】:

  • 使用 @jens 解决方案,并使用扩展运算符 {...res} 或不使用它会产生相同的结果。我的假设是 res 是一个基于 httpclient 文档的对象
  • 更新将导致此错误“找不到类型为'object'的不同支持对象'[object Object]'。NgFor 仅支持绑定到Iterables,例如数组。”
  • 那么你的 res (response) 不是一个数组。请在订阅中显示您的控制台日志。或者查看f12 tab network 看看我返回的服务器有哪些数据
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-07-21
  • 2021-11-01
  • 1970-01-01
  • 1970-01-01
  • 2019-09-14
  • 1970-01-01
  • 2019-05-25
相关资源
最近更新 更多