【发布时间】:2022-02-15 02:10:58
【问题描述】:
我在我的 nestjs 应用程序中调用一个 api。 api 的响应有下面的 hatoas 格式
这是我的代码
import { HttpService, Injectable } from '@nestjs/common';
import { Observable } from 'rxjs';
import { catchError, map, switchMap, tap } from 'rxjs/operators';
@Injectable()
export class IqProductQuantityService {
constructor(
private readonly httpService: HttpService
) {}
...
return this.httpService
.get<any>(url, {
headers: {
Authorization: `Bearer ${token}`
}
})
.pipe(
map((res) => res.data),
tap((data) => console.log(data)),
tap((data) => console.log(data?._links?._next))
);
问题是当我获取数据时,我收到项目数组,并且响应中没有_links或_embeded数据,似乎axios或nestjs正在优雅地解析这些数据以创造生命更容易,但同时我们丢失了_links.next的信息来进行分页
这是我收到的:
console.log(data):
[
{
Id: '82cf8651-c742-4352-aa70-001ee180707c',
CompanyId: 13279,
EntityId: 22235,
IsSerialized: false,
IsDropShippable: false,
IsLot: false,
QuantityInStock: 0,
QuantityOnOrder: 0,
QuantityTransferIn: 0,
QuantityTransferOut: 0,
UnitId: 0
},
{
Id: '82cf8651-c742-4352-aa70-001ee180707c',
CompanyId: 13279,
EntityId: 22236,
IsSerialized: false,
IsDropShippable: false,
IsLot: false,
QuantityInStock: 0,
QuantityOnOrder: 0,
QuantityTransferIn: 0,
QuantityTransferOut: 0,
UnitId: 0
},
{
Id: '82cf8651-c742-4352-aa70-001ee180707c',
CompanyId: 13279,
EntityId: 22237,
IsSerialized: false,
IsDropShippable: false,
IsLot: false,
QuantityInStock: 0,
QuantityOnOrder: 0,
QuantityTransferIn: 0,
QuantityTransferOut: 0,
UnitId: 0
}
]
console.log(data?._links?._next):
undefined
问题是我应该如何检索_links.next.href 数据?
【问题讨论】:
-
this.httpService是如何定义的?好像您正在使用 RxJS 的 .pipe/map/tap。
-
@JonathanRosa 它是nestjs HttpService,它在neath 下使用axios,我更新了问题
-
我有点困惑 -
console.log(data)的输出是指您问题中的 json-array 吗?还是console.log(data?._links?._next)的输出? -
@Reza 我的意思是你的嵌套端代码,而不是你的客户端服务。那里发生了什么? NestJS 和 axios 都没有去掉下划线的属性。
-
@JonathanRosa 好的,让我创建 stackblitz 来复制和分享
标签: node.js axios nestjs hateoas