कैसे उपयोग करने के लिए नमूदार डेटा से एक बाहरी एपीआई में nestjs?

0

सवाल

Im की कोशिश कर रहा से उपयोग करने के लिए एक बाहरी एपीआई में nestjs के साथ 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));
  }
}

और मेरी सेवा वर्ग इस तरह दिखता है:

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

लेकिन अगर मैं console.लॉग मूल्य "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

मैं मेरी समस्या का हल इस के साथ, मुझे आशा है कि यह हो सकता है अपनी आवश्यकताओं फिट.

यदि आप अपनी नमूदार एक वादा के रूप में दो समाधान कर रहे हैं कि हो सकता है आप के लिए फिट.

कक्षा में आप कर रहे हैं का उपयोग कर एक बाहरी एपीआई:

जोड़ें 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ý
..................................................................................................................

इस श्रेणी में लोकप्रिय

लोकप्रिय सवाल इस श्रेणी में