사용하는 방법을 관찰 가능한 데이터를 외부 api 를 nestjs?

0

질문

Im 를 사용하려서 외부 api 를 nestjs 진 날개로 연기 드리프트 같이하지만.

@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"이것은 결과는 다음과 같다:

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

고 그것은 두번째 이후 그것은 아직 해결되지 않. 그것은 가능한 결과를 기다릴 때까지 그것의 해결(i 없 config 에 대한 HttpModule)? 반환이 실제로 개체"infoPrestacion"그러나 나는 일할 필요가 가진 값을 반환하지 않는 객체입니다.

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

최고의 응답

0

내 문제를 해결이,나 희망이 있습니다.

는 경우에 당신은 당신의 현저한 약속으로 두 가지가 있는 솔루션을 맞을 수도 있습니다.

클래스에서 당신은 당신을 사용하여 외부 api 를:

추가 lastValueFrom 로 변환하는 관찰을 약속 가입하여 현저하를 기다리고,그것은 완료,그리고 해결하는 반환되는 약속과 함께 마지막 값에서 관찰된다.

firstValueFrom 수도 솔루션은 그 반대의 lastValuefrom 얻으로 첫 번째 요소의 약속가 해결됩니다.

@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

다른 언어로

이 페이지는 다른 언어로되어 있습니다

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