1

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

1 Answer 1

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 => {
        });
Sign up to request clarification or add additional context in comments.

1 Comment

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...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.