Nestjs'de harici bir apı'den gözlemlenebilir veriler nasıl kullanılır?

0

Soru

Axios ile nestjs'de harici bir apı'den kullanmaya çalışıyorum.

@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));
  }
}

Ve hizmet sınıfım şuna benziyor:

@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;
    }
}

Ancak teselli edersem."ınfoPrestacion" değerini günlüğe kaydet bu sonuçtur:

{
  infoPrestacion: Observable {
    source: Observable { _subscribe: [Function (anonymous)] },
    operator: [Function (anonymous)]
  }
}

ve henüz çözülmediği için ikinciye ulaşmıyor. Çözülene kadar sonucu beklemek mümkün mü (HttpModule için herhangi bir yapılandırmam yok) ? Dönüş aslında nesnenin kendisini "ınfoPrestacion" alır, ancak değerlerle çalışmam ve bu nesneyi döndürmem gerekir.

axios nestjs nestjs-config
2021-11-23 15:25:15
1

En iyi cevabı

0

Sorunumu bununla çözdüm, umarım bu sizin ihtiyaçlarınıza uygun olur.

Gözlemlenebilirliğinizi bir söz olarak alırsanız, sizin için uygun olabilecek iki çözüm vardır.

Sınıfta harici bir apı kullanıyorsunuz:

Gözlemlenebilir'e abone olarak, tamamlanmasını bekleyerek ve döndürülen sözü gözlemlenen akıştan son değerle çözerek bir gözlemlenebilir'i bir söze dönüştüren lastValueFrom ekleyin.

firstValueFrom da bir çözüm olabilir, sözünüz çözüldüğünde ilk öğeyi almaktan lastValuefrom tersini yapar.

@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 lastValueFrom(this.httpService.get(`${this.API}/prestacion/${id}`, this.header).pipe(map((res) => res.data)));
  }
}
2021-11-26 13:34:41

Diğer dillerde

Bu sayfa diğer dillerde

Русский
..................................................................................................................
Italiano
..................................................................................................................
Polski
..................................................................................................................
Română
..................................................................................................................
한국어
..................................................................................................................
हिन्दी
..................................................................................................................
Français
..................................................................................................................
Česk
..................................................................................................................
Português
..................................................................................................................
ไทย
..................................................................................................................
中文
..................................................................................................................
Español
..................................................................................................................
Slovenský
..................................................................................................................