0
save(body) {
  let headers = new HttpHeaders()
  headers.append('Content-Type', 'text/plain')
  return this.callWorkflowService
    .getRawDataflow()
    .pipe(
      map(data => {
        data['jobs'] = body
        return data
      })
    )
    .pipe(newData => // correspond to `return data` 
      this.httpClient
        .post(this.url, JSON.stringify(newData), { headers: headers })
        .pipe(
          map(res => {
            return res
          })
        )
    )
}

What I'm getting actually inside the httpRequest is an Observable instead of an Object. I don't understand why.

2
  • do you subscribe to your saveBody ? httpClient.post always return an observable Commented Dec 31, 2019 at 14:59
  • yes I subscribe to it in component Commented Dec 31, 2019 at 15:51

2 Answers 2

2

Here when you are chaining multiple Observables you are creating a Observable of Observables. So you need to flatten them before you chain them. RxJS provides multiple flattening operators like switchMap , mergeMap, concatMap and exhaustMap for flattening Observables.

  save(body) {
    let headers = new HttpHeaders();
    headers.append('Content-Type', 'text/plain');
    return this.callWorkflowService.getRawDataflow().pipe(
      map(data => ({data, ...{"jobs": body}})),
      mergeMap(newData =>
        this.httpClient.post(this.url, JSON.stringify(newData), {
          headers: headers
        })
      )
    );
  }

This is a wonderful article which talks about flattening of Observables.

Sign up to request clarification or add additional context in comments.

Comments

1

you should use switch map operator for example

 return this.callWorkflowService
    .getRawDataflow()
    .pipe(
      map(data => {
        data['jobs'] = body
        return data
      }),
    switchMap(() => this.httpClient
        .post(this.url, JSON.stringify(newData), { headers: headers })
        .pipe(
          map(res => {
            return res
          })
        ))
    )

Comments

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.