英文:
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
英文:
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 '@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)
} }
And my component.ts is
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);
}
}
And this is my component.html
<p *ngFor="let emploee of emploee$">{{emploee.employee_name }}</p>
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
答案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<Emploee[]>{
return this.http.get(this._url).pipe(
map(value => 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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论