javascript - 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);
解决方案
尝试这个:
findAll(): Observable<Cat[]> {
return this.httpService.get('http://localhost:3000/cats')
.pipe(map(response => response.data);
}
推荐阅读
- html - sending parsed JSON file to client but format is not HTML
- javascript - 有什么办法可以让带有输入和排序的工具提示都应用于VueJS中的同一个标题?
- python - 我如何知道 Python 是否会在 Spyder IDE 中引发异常?
- python - 如何修复 Import object_detection/protos/image_resizer.proto 但未使用
- angular - Ngx-guage 百分比值
- c++ - 如何在cryptoki中重命名容器名称
- unit-testing - 是否可以选择在 Spock 中动态生成 n 个测试
- dbt - dbt可以连接同一个项目的不同数据库吗?
- reactjs - 为什么 React 菜单选择不保持选择?
- javascript - 如何修复弹性框的文本溢出