angular - 如何从 observable 中获取正确的数据
问题描述
我正在尝试从可观察到的数据中获取数据,但我没有得到正确的数据。这是我对我的 API 的调用
getLastxScans(amount: number): Observable<Scan[]> {
return this.http.get<Scan[]>(`${this.ROOT_URL}/Scan/amount/${amount}`);
}
当我订阅它时它会返回这个
{scans: Array(30)}
当我尝试用此数据填充 Scan[] 时,它不起作用。我是否需要更深入地了解对象以提取数据?如果是这样,我该怎么做?我试过这样做
this.deviceService.getLastxScans(this.scanAmount)
.subscribe(scans => this.scans = scans);
而这种方式
this.deviceService.getLastxScans(this.scanAmount)
.subscribe(res => this.scans = res.scans);
解决方案
使用:(因为数组是scans
响应的key的值)
this.deviceService.getLastxScans(this.scanAmount)
.subscribe(res => this.scans = res.scans);
从评论中,您还可以执行以下操作:
getLastxScans(amount: number): Observable<Scan[]> {
return this.http.get<Scan[]>(`${this.ROOT_URL}/Scan/amount/${amount}`)
.pipe(
map((res:any) => res.scans)
);
}
推荐阅读
- c++ - 赋值运算符重载
- angular - 将 Angular 组件注入到 FullCalendar 事件条目中
- css - Primefaces 日历不会重新调整其在窗口旋转时的位置
- java - Java - 仅返回 Else 子句的 IF 语句
- if-statement - 关于 xamarin 表单的 if 语句
- javascript - 在 Neo4j 中使用 UNWIND 语句创建节点时如何指定要使用的记录?
- python - 如何在 numpy 数组中找到相同的实体?
- html - .svg 图片未显示
- javascript - 在 JQuery 中迭代属性长度等于数字的对象属性
- python - 我无法对我的数组求和,因为我认为它被存储为多个数组