try to fetch data from api in angular but get error that Cannot find a differ supporting object '[object Object]' of type 'object'

huangapple go评论83阅读模式
英文:

try to fetch data from api in angular but get error that Cannot find a differ supporting object '[object Object]' of type 'object'

问题

我的Angular版本是16.0.1,我尝试从Api中获取数据

https://dummy.restapiexample.com/api/v1/employees

这是我的模型

export interface Emploee {
  employee_name: string;
}

我的服务

import { HttpClient } from '@angular/common/http';
import { Injectable } from '@angular/core';
import { Observable } from 'rxjs';
import { Emploee } from '../models/emploee.model';

@Injectable({
  providedIn: 'root'
})
export class EmploeeService {
  _url = "https://dummy.restapiexample.com/api/v1/employees"
  constructor(private http: HttpClient) { }
  getEmploee(): Observable<Emploee[]> {
    return this.http.get<Emploee[]>(this._url)
  }
}

而我的组件.ts

import { Component } from '@angular/core';
import { Emploee } from '../models/emploee.model';
import { EmploeeService } from '../services/emploee.service';

@Component({
  selector: 'app-emploee',
  templateUrl: './emploee.component.html',
  styleUrls: ['./emploee.component.scss']
})
export class EmploeeComponent {
  emploee$!: Emploee[];
  ngOnInit() {
    this.fetchEmploee();
  }
  constructor(private emploeeService: EmploeeService) { }
  fetchEmploee() {
    this.emploeeService.getEmploee().subscribe(res => this.emploee$ = res);
  }
}

这是我的组件.html

<p *ngFor="let emploee of emploee$">{{emploee.employee_name }}</p>

当我运行我的项目时,我收到以下错误

找不到与类型为'object'的对象'[object Object]'的差异支持。NgFor只支持绑定到可迭代对象,如数组

我该如何解决这个问题?

另外,我的项目在这个链接上:https://stackblitz.com/edit/stackblitz-starters-xuvgju?embed=1&file=src%2Fmain.ts

而来自Postman的图片如下所示:
try to fetch data from api in angular but get error that Cannot find a differ supporting object '[object Object]' of type 'object'

英文:

My Angular version is 16.0.1 and I try fetch data from Api

https://dummy.restapiexample.com/api/v1/employees

this is my Model

export interface Emploee {
employee_name: string;
}

My service is

import { HttpClient } from &#39;@angular/common/http&#39;;
import { Injectable } from &#39;@angular/core&#39;;
import { Observable } from &#39;rxjs&#39;;
import { Emploee } from &#39;../models/emploee.model&#39;;

@Injectable({
providedIn: &#39;root&#39;
})
export class EmploeeService {
_url = &quot;https://dummy.restapiexample.com/api/v1/employees&quot;
constructor(private http:HttpClient) { }
getEmploee():Observable&lt;Emploee[]&gt;{
  return this.http.get&lt;Emploee[]&gt;(this._url)
} }

And my component.ts is

import { Component } from &#39;@angular/core&#39;;
import { Emploee } from &#39;../models/emploee.model&#39;;
import { EmploeeService } from &#39;../services/emploee.service&#39;;

@Component({
selector: &#39;app-emploee&#39;,
templateUrl: &#39;./emploee.component.html&#39;,
styleUrls: [&#39;./emploee.component.scss&#39;]
    })
export class EmploeeComponent {
 emploee$! : Emploee[];
ngOnInit() {
this.fetchEmploee();
   }
constructor(private emploeeService:EmploeeService){}
fetchEmploee(){
this.emploeeService.getEmploee().subscribe(res=&gt; this.emploee$ = res);
   }
   }

And this is my component.html

&lt;p *ngFor=&quot;let emploee of emploee$&quot;&gt;{{emploee.employee_name }}&lt;/p&gt;

when I run my project i get this error

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

How can I resolve that?
also my project is in this link :https://stackblitz.com/edit/stackblitz-starters-xuvgju?embed=1&file=src%2Fmain.ts

and picture from postman is:
try to fetch data from api in angular but get error that Cannot find a differ supporting object '[object Object]' of type 'object'

答案1

得分: 2

First, normally we use the $ prefix to indicate an Observable. You are using it on an array, which is confusing for anyone else looking at your code.

我通常们使用 "$" 前缀来表示一个 Observable。你在一个数组上使用它,这对其他人来说很令人困惑。

I thought you were missing the async pipe because I thought that the emploee$ variable was an observable.

我以为你漏掉了 async 管道,因为我以为 emploee$ 变量是一个 Observable。

But the actual problem is that the data being returned is not of the shape you have defined.

但实际的问题是返回的数据不符合你定义的形状。

If you look at the data shape, it contains much more data then just the Emploee data.

如果你查看数据的形状,它包含的数据远不止 Emploee 数据。

Consider updating your interface to match the data shape.

考虑更新你的接口以匹配数据的形状。

Alternately, you could modify your getEmploee to access just the "data" property which seems to hold the actual employee data.

或者,你可以修改你的 getEmploee 来访问只有 "data" 属性,这个属性似乎包含了实际的员工数据。

getEmploee(): Observable<Emploee[]> {
  return this.http.get(this._url).pipe(
    map(value => value.data)
  );
}

我在这里有一个可工作的 Stackblitz:https://stackblitz.com/edit/stackblitz-starters-5uprkt

我在这个视频中提供了更多关于 RxJS 的信息:https://youtu.be/vtCDRiG__D4

英文:

First, normally we use the $ prefix to indicate an Observable. You are using it on an array, which is confusing for anyone else looking at your code.

I thought you were missing the async pipe because I thought that the emploee$ variable was an observable.

But the actual problem is that the data being returned is not of the shape you have defined.

If you look at the data shape, it contains much more data then just the Emploee data.

Consider updating your interface to match the data shape.

Alternately, you could modify your getEmploee to access just the "data" property which seems to hold the actual employee data.

getEmploee():Observable&lt;Emploee[]&gt;{
  return this.http.get(this._url).pipe(
          map(value =&gt; value.data)
  );
}

I have a working Stackblitz here: https://stackblitz.com/edit/stackblitz-starters-5uprkt

For more information on RxJS, check out this video: https://youtu.be/vtCDRiG__D4

huangapple
  • 本文由 发表于 2023年6月30日 01:05:45
  • 转载请务必保留本文链接:https://go.coder-hub.com/76583202.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定