首页 > 解决方案 > NestJS Http 模块

问题描述

我从 nestJS 文档(https://docs.nestjs.com/techniques/http-module#http-module)中获取了这个示例,这是我的问题的一个最小示例:

@Injectable()
export class CatsService {
  constructor(private httpService: HttpService) {}

  findAll(): Observable<AxiosResponse<Cat[]>> {
    return this.httpService.get('http://localhost:3000/cats');
  }
}

如何从 Observable<AxiosResponse<Cat[]>> 中提取实际的猫数组?我尝试了以下方法,但它给了我一个订阅者对象,我也不知道如何打开它来获取实际数据。

const cats = await this.catsService.findAll().subscribe((val) => val);

标签: javascripttypescriptaxiosnestjs

解决方案


尝试这个:

  findAll(): Observable<Cat[]> {
    return this.httpService.get('http://localhost:3000/cats')
      .pipe(map(response => response.data);
  }

推荐阅读