[英]how to fix this message angular observable
I have a little probleme, I use the node(express) and angular, I want display all classes in angular but it's show an error like this 我有一点问题,我使用节点(快速)和角度,我希望显示角度的所有类,但它显示这样的错误
and it's not show json of back-end, what is wrong I don't understand 而且它没有显示后端的json,我不明白有什么不对
Return of method http get clasRoutes.js 返回方法http get clasRoutes.js
res.json({ success: true, classes: postQuery, total: nbClasses });
Clas.interface.ts Clas.interface.ts
export interface Clas {
room: string;
img?: string;
}
Clas.service.ts Clas.service.ts
private urlGetClasses = environment.url + 'classes';
constructor( private http: HttpClient ) { }
getClass(): Observable<Clas[]> {
return this.http.get(this.urlGetClasses).pipe(map(a => console.log(a)));
}
Clas.component.ts Clas.component.ts
public clas: Clas[] = [];
constructor(private clasService: ClasService) {}
ngOnInit() {
this.getClass().subscribe(res => {
this.clas = res.classes
});
}
private getClass(): Observable < Clas[] > {
return this.clasService.getClass().pipe(tap((value) => {
console.log(value)
}));
}
Clas.component.html Clas.component.html
<p *ngFor="let cla of clas">
zefd {{cla.room}}
</p>
There is new RxJS syntax. 有新的RxJS语法。 You should write it as
你应该把它写成
private getClass(): Observable<Clas[]> {
return this.clasService.getClass().pipe(tap((value) => {console.log(value)}));
}
make sure you import pipe
and tap
from rxjs
. 确保从
rxjs
导入pipe
并tap
。
The problem is with Clas.service.ts
. 问题出在
Clas.service.ts
。 In your getClass()
method, you are mapping to void as the return type of console.log
is void; 在
getClass()
方法中,您将映射到void,因为console.log
的返回类型为void; ie you are returning void whereas you've specified the return type of getClas()
is Observable<Clas[]>
. 即你返回void而你已经指定了
getClas()
的返回类型是Observable<Clas[]>
。 Therefore, you need to change your getClas()
method to: 因此,您需要将
getClas()
方法更改为:
getClass(): Observable<Clas[]> {
return this.http.get(this.urlGetClasses);
}
Or as Nikola mentioned, you can use tap
to log like so: 或者正如Nikola所提到的,你可以使用
tap
来记录:
private getClass(): Observable<Clas[]> {
return this.clasService.getClass().pipe(tap((value) => {console.log(value)}));
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.