1

I have a service in which it either stores an object so I can access it in other components or requests data from a web api and returns it in a map.

Example get for the objectService

get(): any {
    if (this.object == undefined || this.object == null) {
        return this.http.get(this.baseUrl + "/companies/get)
            .map((data: any) => {
                this.object = data;
                return this.object;
            });
    } else {
        return this.object;
    }
}

Using the service to set page title

ngOnInit() {
    this.objectService.get().subscribe((data: any) => {
        this.title.setTitle(data.name);
    });
}

So this works when I first load the page, but upon navigating to the page without a reload I get the following error ...subscribe is not a function. Which makes sense since I am returning an object and not an http request.

So I tried to return just the object and not a http request.

Example get for the objectService

get(): any {
    if (this.object == undefined || this.object == null) {
        return this.http.get(this.baseUrl + "/companies/get)
            .map((data: any) => {
                this.object = data;
            });
    }
    return this.object;
}

Using the service to set page title

ngOnInit() {
    this.title.setTitle(this.objectService.get().name);
}

This now flips the issue where page refresh gives me an error saying this.objectService.get().name is undefined and when I navigate to my component without a page refresh it works just fine.

Any help on trying to get this to work would be appreciated. Thanks in advance!

1
  • 1
    convert the normal object to an observable by Observable.of('this.object') Commented Sep 28, 2017 at 18:32

1 Answer 1

1

Actually you are mixing the observable and normal object!

I would say stick to the first aproach but while returning just the object, convert it to an observable so that it will work in your onNgInit

So your service will

get(): Observable<any> {
    if (this.object == undefined || this.object == null) {
        return this.http.get(this.baseUrl + "/companies/get)
            .map((data: any) => {
                this.object = data;
                return this.object;
            });
    } else {
        return Observable.of(this.object);
    }
}

And your consumption will be still the normal and work for both the cases.

ngOnInit() {
    this.objectService.get().subscribe(
      (data: any) => {  // this for response data
        this.title.setTitle(data.name);
      },
      (error: any) => {  // this for error
        console.log('error: %o', error);
      },
   );
}
Sign up to request clarification or add additional context in comments.

3 Comments

Lets say the http request returns 404. How would I detect this in my ngOnInit function?
Nvm figured it out, I set a catch after the map in the service and I am able to return the error and check for it when I consume the service.
you can also do it in component end by passing the second function to subscribe.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.