A. Gladkiy
A. Gladkiy

Reputation: 3450

Angular 5 convert Observable<obj> to obj

I'm implementing resolve method:

resolve(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable<Obj> | Obj {
        return this.service.get(route.params['guid'])
            .map((response: Json) => {
                return this.service.list(response.data.id).map((response: Json) => {
                    return <Obj>response.data;
                });
        });

But I have error Can't convert to Observable<Observable<Obj>>.

How can I resolve that issue?

Upvotes: 0

Views: 68

Answers (2)

rguerin
rguerin

Reputation: 2128

The result inside a map is already an Observable. Try this :

resolve(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable<Obj> | Obj {
        return this.service.get(route.params['guid'])
            .mergeMap((response: Json) => {
                return this.service.list(response.data.id).map((response: Json) => {
                    return response.data;
                });
        });

EDIT: @Rafael is right, you must also use mergeMap (of flapMap if you are using a rxjs version < 6), I edited my post.

Upvotes: 0

Rafael
Rafael

Reputation: 7746

You can't make this return an Obj because it's async. Therefore, it must be unwrapped. You can make it an Observable<Obj> by using mergeMap:

resolve(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable<Obj> {
        return this.service.get(route.params['guid'])
            .mergeMap((response: Json) => {
                return this.service.list(response.data.id).map((response: Json) => {
                    return response.data;
                });

Upvotes: 1

Related Questions