如何从可观察对象中获取正确的数据
How do I get the correct data out of a observable
我正在尝试从可观察对象中获取数据,但我没有获取正确的数据。
这是我打给 API
的电话
getLastxScans(amount: number): Observable<Scan[]> {
return this.http.get<Scan[]>(`${this.ROOT_URL}/Scan/amount/${amount}`);
}
我订阅的时候returns这个
{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
键的值)
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)
);
}
我正在尝试从可观察对象中获取数据,但我没有获取正确的数据。 这是我打给 API
的电话 getLastxScans(amount: number): Observable<Scan[]> {
return this.http.get<Scan[]>(`${this.ROOT_URL}/Scan/amount/${amount}`);
}
我订阅的时候returns这个
{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
键的值)
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)
);
}