0

This question is related to my previous question. The issue I'm facing is in sending the data to back-end using two services.

I have read about using forkJoin of Observables but I am not able to use it for my problem.

My add() method:

add() {
    this.employeesService
    .createEmployee(this.form.value as Employee)
    .subscribe(
        employee=> {
            console.log(employee);
        },
        (err: HttpErrorResponse) => {
            this.messagesService.openDialog('Error', 'Invalid Employee. Try Again!');
        }
    );
    const resources: any = {};
    const employeeId: any = {};
    let wholeData: any = [];
    function makeArray(resourceNumber, id) {
        resources.push(resourceNumber);
        for(var i = 0; i < this.resourceNumber.length; i++) {
            employeeId.push(id);
        }
    }
    wholeData = new makeArray(this.resourceNumbers, employeeId);
    this.resourceNumbersService
    .createResourceNumber(wholeData as Resource)
    .subscribe(
        resourceNumber => {
            console.log(resourceNumber);
        },
        (err: HttpErrorResponse) => {
            this.messagesService.openDialog('Error', 'Invalid Resource Number. Try Again!');
        }
    );
}

This is the Postman Json format for resources which is doing well with my backend:

{ "resources": [ { "resourceNumber": "121", "employeeId": "3" }, 
{ "resourceNumber": "122",  "employeeId": "3" }, 
{ "resourceNumber": "123", "employeeId": "3"} ] }

The trouble I'm facing is in sending the data to resources database. The Employees are sent totally fine. Is there any easier approach? Many thanks. Kindly find the problem on Stackblitz

Rachit
  • 79
  • 1
  • 12

1 Answers1

0

I have read about using forkJoin of Observables but I am not able to use it for my problem.

So following is the way to use forkJoin

import { forkJoin } from 'rxjs';  // RxJS 6 syntax


  functionReturningForkJoinOFObservableBatch(): Observable<any[]> {
    let observableBatch = [];
    observableBatch.push("push all observables ")
    return forkJoin(...observableBatch);
  }

   //subscribe the above function to get response of all observable in single go

   //When all observables complete, emit the values

     this.functionReturningForkJoinOFObservableBatch().subscribe(responseList => {
        });
Amrit
  • 2,115
  • 1
  • 21
  • 41
  • Hey @Amrit I am not getting a good idea of how to implement that code in my problem. Could you please check my updated question and have a look into the stackblitz problem... – Rachit Jul 29 '19 at 04:30