Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to correctly return a void Observable?

Tags:

angular

rxjs

In my Angular 4 application, I have a method that have to return an Observable. This method has 3 conditions. First and second conditions make a get call, but the third condition does nothing and in this case I have to return an Observable as well, because this method is the first part of a .flatmap operator. So to chain the second part of the .flatmap operator, I need an Observable from the first part.

I've tried return new Observable<void>();, but I've got an error:

ERROR TypeError: Cannot read property 'subscribe' of undefined

This is the initial method that calls a service to load the data.

loadModelData() {       this.customerService.getModelDetail(this.code)         .subscribe(response => {           this.selected = response.body as Customer;         });     }   } 

This is the service method that have to chain 2 calls.

getModelDetail(code) {      const params = new HttpParams().set('projection', 'withBalance');      return this.endPointUrlService.loadMaps(this.entityLink)        .flatMap((res) => {         return this.http.get(this.endPointUrlService.cutLinks(           this.endPointUrlService.map.get('customers')) + '/' + code, {observe: 'response', params: params})           .map((response) => <any>response);       })   } 

And this is the methods from a support service. checkIfMapIsReady() is a method that returns a void Observable in the third case:

  loadMaps(entityLink: EntityLink[]): Observable<void> {     console.log(entityLink);      for (const i in entityLink) {       if (entityLink.hasOwnProperty(i)) {       return this.checkIfMapIsReady(entityLink[i].name, entityLink[i].searchMap, entityLink[i].endPoints[0])       }     }   }     public checkIfMapIsReady(modelName: string, mapName: string, endPoints: string) {      if (this.map.get(modelName) === undefined) {       console.log('endpoint url service All maps undefined ' + modelName);       return this.getLinks(modelName, this.mapNames.get(mapName), false)     } else {       console.log('endpoint url service Populate only ' + mapName);       if (this.mapNames.get(mapName).get(endPoints) === undefined) {       return  this.getLinks(modelName, this.mapNames.get(mapName), true)       } else {         return new Observable<void>();       }     }   } 
like image 358
Alessandro Celeghin Avatar asked Nov 05 '17 11:11

Alessandro Celeghin


People also ask

How do I return a blank observable?

To return an empty Observable with Rxjs, we can use EMPTY or of . import { EMPTY, empty, of } from "rxjs"; EMPTY; of({}); to import EMPTY which is an empty observable. We can also call of with an empty object to return an empty observable.

What is the return type of observable?

http. get(apiURL) returns an Observable . map is an observable operator which calls a function for each item on its input stream and pushes the result of the function to its output stream.

What is an empty observable?

A simple Observable that emits no items to the Observer and immediately emits a complete notification.

What does observable subscribe return?

Subscriptions to observables are quite similar to calling a function. But where observables are different is in their ability to return multiple values called streams (a stream is a sequence of data over time). Observables not only able to return a value synchronously, but also asynchronously.


1 Answers

return of(); doesn't create a stream.

Whereas

return of(void 0);

does.

if you are mocking and need to return a stream of void. Like my life. [Sad Panda]

like image 125
jenson-button-event Avatar answered Sep 22 '22 11:09

jenson-button-event