Im cercando di utilizzare una api esterna in nestjs con axios.
@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));
}
}
E la classe di servizio simile a questo:
@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;
}
}
Tuttavia, se i console.registro il valore "infoPrestacion" questo è il risultato:
{
infoPrestacion: Observable {
source: Observable { _subscribe: [Function (anonymous)] },
operator: [Function (anonymous)]
}
}
e doesnt ottenere il secondo visto che non è ancora risolto. È possibile attendere il risultato fino a quando è stato risolto (non ho una config per l'HttpModule) ? Il ritorno diventa l'oggetto stesso "infoPrestacion" ma ho bisogno di lavorare con i valori e non restituire l'oggetto.