Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

242
Views
Angular: uso de mergeMap para obtener datos de 2 API y guardarlos en una matriz

Escenario: en ngOnInit, necesito llamar a 3 API, Fork-Join y guardar datos en una matriz. La primera API es independiente. La segunda también es independiente, pero la tercera depende de la respuesta de la segunda API.

No he trabajado antes con forkjoin o mergemap y estoy un poco confundido.

Qué esta pasando :

Creé un método común que toma un identificador para averiguar qué punto final alcanzar y el objeto con los parámetros requeridos y luego hay respuesta y manejo de errores.

Archivo de servicio:

 callMethod(str, obj, cb?) { return new Observable(observer => { this.httpService.getHttpParams(this.URLS_CONSTANT[str], obj, res => { observer.next(res.data.content ? res.data.content : res.data); observer.complete(); }, err => { }) }) }

Por lo tanto, puedo obtener la respuesta de las 3 API, pero el resultado final después de la combinación de bifurcación contiene la respuesta de la 1.ª y la 3.ª API. falta la respuesta de 2nd, y no sé qué está pasando.

Este es el método que escribí:

 fetchAllData() { let inp = this.input_row; let response1 = this.service.callMethod('rate_plan', { companyId: inp.cpnyId, servAbbr: inp.servAbbr, productNumber: inp.prdctNo, orgNumber: inp.orgNo, rateCategory: inp.rateCat, ratePlanNumber: inp.ratePlanNo }); let response2 = this.service.callMethod('approve_rate', { prodctNo: inp.prdctNo, servAbbr: inp.servAbbr, companyId: inp.cpnyId, ratePlanNo: inp.ratePlanNo }).pipe(map(item => { this.approveRate = item let appRate = item[0] return appRate }), mergeMap(data => this.service.callMethod('rate_detail', { companyId: inp.cpnyId, ratePlanNo: inp.ratePlanNo, rateCaseGrpngNo: data['rateCaseGrpngNo']})) ) forkJoin([response1, response2]).subscribe(x => console.log(x)) } }

La consola final tiene valores de respuesta de la llamada API 1 y 3. Qué cambio se requiere para crear una matriz con tres objetos separados. Déjame saber si se requiere alguna aclaración más.

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Su enfoque es perfecto, pero la segunda respuesta se puede ejecutar a través de un switchMap para obtener la tercera respuesta, luego ejecutar sobre un mapa, para devolver los valores individuales de secondResponse y thirdResponse dentro de una matriz.

 fetchAllData() { let inp = this.input_row; let response1 = this.service.callMethod('rate_plan', { companyId: inp.cpnyId, servAbbr: inp.servAbbr, productNumber: inp.prdctNo, orgNumber: inp.orgNo, rateCategory: inp.rateCat, ratePlanNumber: inp.ratePlanNo, }); let response2 = this.service .callMethod('approve_rate', { prodctNo: inp.prdctNo, servAbbr: inp.servAbbr, companyId: inp.cpnyId, ratePlanNo: inp.ratePlanNo, }) .pipe( switchMap(responsesecond => { this.approveRate = responsesecond; let appRate = responsesecond[0]; return this.service .callMethod('rate_detail', { companyId: inp.cpnyId, ratePlanNo: inp.ratePlanNo, rateCaseGrpngNo: responsesecond['rateCaseGrpngNo'], }) .pipe( map(responsethird => { return [responsesecond, responsethird]; }) ); }) ); forkJoin([response1, response2]).subscribe(x => console.log(x)); }
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!