如何使用可观察的数据,从外部api在nestjs?

0

的问题

我试图使用一个外部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)]
  }
}

它不会得到第二,因为它没有得到解决。 是否有可能等待的结果,直至它得到解决(我没有任何配置为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ý
..................................................................................................................