简体   繁体   中英

How to construct data, from Observables value, and return a Observable<Response> after the HTTP call?

When constructing a data object that contains values which are resolved from an Observable, I'm unable to figure how to return the Observable<Response> .

I've tried using the forkJoin() , to resolve the values first, and then subscribe to it, and return the this.http.post()... call. This, however, makes it not possible to subscribe to the function itself fetchData().subscribe( ... ) . I'm clearly missing something.

So, how do I construct a data object, which contains resolved Observable values, and subsequently return the Observable<Response> object?

// Simplified example 

fetchData() { 

  // Construct the data
  var data = {
      v1 : this.getValueFromObservable1(),
      v2 : this.getValueFromObservable2()
  };

  // Make the call, return the Observable<Response> object
  return this.http
      .post( 'api/authorization', JSON.stringify(data) );
}

This should do what you want:

fetchData() {
    return this.getValueFromObservable1()
    .mergeMap(v1 => 
          this.getValueFromObservable2()
          .map(v2 => { v1: v1, v2: v2})
    );
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM