user630209
user630209

Reputation: 1207

Type 'Subscription' is not assignable to type 'Observable' error while return from subscribe

    setPrivileges(privilege: string): Observable<boolean> {
        if (this.loginService.privilegeList != null && this.loginService.privilegeList != undefined && this.loginService.privilegeList.getValue() != null) {
            this.privileges = this.loginService.privilegeList.getValue().filter(item => item.name === privilege);
            console.log("if this.privileges : "+this.privileges);
                return Observable.of(true);
        } else {
              return this.getUserRolesAndMenus().subscribe((result: any) => {  // line 7
               console.log("result  :"+JSON.stringify(result));
               this.privileges = result.roles.filter((element: any) => {
                    element.privileges.filter((item: any) => {
                        item.name === privilege;
                    })
                })
                console.log("esle this.privileges : "+this.privileges);
                if(this.privileges == null)
                    return Observable.of(true);
                else
                       return Observable.of(true);
            });
        }
    }

On line 7 it throws exception as

[ts]

Type 'Subscription' is not assignable to type 'Observable'. Property '_isScalar' is missing in type 'Subscription'. (method) UserService.getUserRolesAndMenus(): Observable To get the user details. '

What is doing wrong here ?

/**
     * To get the user details.
     */
    getUserRolesAndMenus(): Observable<any> {
        let headers = new Headers();
        headers.append(AppUtils.HEADER_AUTHENTICATION, localStorage.getItem(AppUtils.STORAGE_ACCOUNT_TOKEN));
        return this.http.get(AppUtils.GET_USER_ROLES_AND_MENUS + AppUtils.SYSTEM_ID_IRS, { headers: headers })
            .map(response => response.json())
            .catch(this.handleError)
           /* .publishReplay(1)
            .refCount();*/
    }

Upvotes: 1

Views: 2015

Answers (1)

Robin Dijkhof
Robin Dijkhof

Reputation: 19278

You are returning a subscription instead of a Observable. Change subscribe to switchMap. This will return an Observable and replaces the result of getUserRolesAndMenus by the Observable you create with Observable.of(...)

Upvotes: 2

Related Questions