I have a service with some properties, which to be initialized before any component load (they used as parameters to other http calls). Route CanActivate method used to call the following method, which should retrieve true after properties initialized, or false on error. I receive compilation error: A function whose declared type is neither 'void' nor 'any' must return a value. How I can avoid this error?
public setupSession(): boolean {
this._http.get('myservice_url')
.map (
(response: Response) => <any>response.json())
.subscribe (
data => {
this.property1 = data.property1;
this.property2 = data.property2;
return true;
},
err => {
console.error(err)
return false;
}
)
}
Edit: @K. Daniek thanks for help! I found solution with help of this article link. Now I can ensure setup complete before any other http call.
The service method:
public setupSession(): Promise<any> {
var observable = this._http.get('myservice_url')
.map((response: Response) => {
var res = response.json();
return res;
});
observable.subscribe(data => {
this.property1 = data.property1;
this.property2 = data.property2;
});
return observable.toPromise();
}
canActivate method:
canActivate() {
return this._sessionService.setupSession().then(() => {
return true;
});
}