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;
}
}
ただし、値「infoPrestacion」を console.log に記録すると、次の結果になります。
{
infoPrestacion: Observable {
source: Observable { _subscribe: [Function (anonymous)] },
operator: [Function (anonymous)]
}
}
まだ解決されていないため、2番目には到達しません。結果が解決されるまで結果を待つことは可能ですか (HttpModule の構成がありません)。戻り値は実際にはオブジェクト自体「infoPrestacion」を取得しますが、値を操作してそのオブジェクトを返さないようにする必要があります。