javascript - Property 'do' does not exist on type 'Subscription' -


what misunderstanding how use .subscribe function in combination .do function?

here observable sequence:

  lookupsubscriber = (text$: observable<string>) =>    text$.debouncetime(300)      .distinctuntilchanged()      .do(() => this.searching = true)      .switchmap(term => {         var data = this._callapi(this.lookupsubscriberapi, term)            .do(() => {               this.searchfailed = false;               this.searching = false;            })            .catch(() => {               this.searchfailed = true;               this.searching = false;               return observable.of([]);            })         return data;      })      .do(() => this.searching = false); 

if _callapi function follows, works:

_callapi(url: string, term: string) {   if (term === '') {      return of.call([]);   }    return map.call(this.dataservice.get(url + term), response => {      var data = this._transformsubscriberinfo(response);      return data;   }) 

}

however, when try rewrite subscribe function this:

   _callapi = (url: string, term: string) => {       return this.dataservice.get(url + term)         .subscribe(            response => { this._transformsubscriberinfo(response) },             error => error.text(),             () => {               if (logging.isenabled.light) {                  console.log('%c api call complete', logging.normal.orange);               }         )) } 

... data call succeeds, receive error: property 'do' not exist on type 'subscription'.

basically trying catch errors , run "always" function after api call, shown in second version of _callapi.

the first version of _callapi seems return observable while second 1 returns subscription object. , subscription not expose do, error message states.

what may want try use version of do accepts error , complete callbacks in addition next callback:

return this.dataservice.get(url + term)     .map(response => this._transformsubscriberinfo(response))     .do(         response => { /* log response */ },          error => { /* log error */ },          () => { /* log completion */ }     ); 

it worth mentioning do not capable of transforming source stream, observable returns contains same values observable called on. that's why need line .map(response => this._transformsubscriberinfo(response)).

also complete callback should not confused "always" function: gets called when source observable completes , not called when observable produces error or gets unsubscribed.


Comments

Popular posts from this blog

node.js - Node js - Trying to send POST request, but it is not loading javascript content -

javascript - Replicate keyboard event with html button -

javascript - Web audio api 5.1 surround example not working in firefox -