【发布时间】:2022-01-02 02:24:03
【问题描述】:
我正在尝试在带有 axios 的 Nestjs 中使用外部 api。
@Injectable()
export class PIntegration {
constructor(private httpService: HttpService) { }
API = process.env.API || 'http://localhost:3000';
header = { headers: { 'Content-Type': 'application/json' } };
async getPrestacionById(id: string): Promise<Observable<IPrestacion>>{
return this.httpService.get(`${this.API}/prestacion/${id}`, this.header).pipe(map((res) => res.data));
}
}
我的服务类如下所示:
@Injectable()
export class ProductService{
constructor(private pIntegration: PIntegration){}
async producto(id: string) {
const infoPrestacion = await this.pIntegration.getPrestacionById(id);
console.log({ infoPrestacion })
if (infoPrestacion)
{
if (infoPrestacion.detalles) {
console.log(infoPrestacion.detalles)
console.log("tiene detalles")
}
}
return infoPrestacion;
}
}
但是,如果我 console.log 值“infoPresacion”,结果如下:
{
infoPrestacion: Observable {
source: Observable { _subscribe: [Function (anonymous)] },
operator: [Function (anonymous)]
}
}
它没有达到第二个,因为它还没有解决。是否可以等待结果解决(我没有 HttpModule 的任何配置)?返回实际上获取对象本身“infoPresacion”,但我需要使用这些值而不是返回该对象。
【问题讨论】:
-
看来你不想使用 rxjs obervables,那就关注这个indepth.dev/posts/1287/…
-
@MicaelLevi 我正在寻找这两种方法,但我用“lastValueFrom”解决了这个问题(看不到该网站,服务器似乎已关闭)
标签: axios nestjs nestjs-config