It appears that the get(url)
method in your code does not actually return anything, causing the issue you are facing.
Furthermore, it is important to avoid explicitly specifying the any
type unnecessarily. By letting the compiler infer the return type (which would be void
), you can prevent errors like this from occurring. Explicitly stating the return type as any
interrupts the type inference process without justification.
While many Angular examples may make use of such practices, they do not necessarily reflect best practices for TypeScript coding.
If the reference to http
in your initial service pertains to the official Angular 2 http service, a better approach would be:
import {Observable} from 'rxjs/Observable';
import 'rxjs/add/observable/fromPromise';
import 'rxjs/add/operator/mergeMap';
export class HttpClient {
get(url: string) {
const headers = new Headers();
this.createGeneralHeaders(headers);
return Observable
.fromPromise(this.storage.get('token'))
.flatMap(
name => this.http.get(`${url}?access-token=${name}`, {headers})
);
}
}
In your original scenario, the get
function was not returning any value. Modifying it to return the result of the then
call would yield a Promise
to an Observable
, instead of directly returning an Observable
. Since the consuming code seemed to expect an Observable
due to the subsequent map
invocation, using Observable.fromPromise
can achieve the desired outcome easily.