简体   繁体   English

如何修复此消息角度可观察

[英]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导入pipetap

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM