John Smith
John Smith

Reputation: 203

Angular2: Observable flatMap error Cannot read property 'subscribe' of undefined

I'm using Angular 2 final version and trying to perform HTTP request that depends on another HTTP request.

My case is trying to getAccount with user id as parameter which returned from another http request (getUser)

AccountService:

constructor(private http: Http, private userService: UserService) {
    }

getAccount(): Observable<Account> {

    let tenant: string = 'test';

    Observable.fromPromise(this.userService.getUser()).flatMap(
        user => {
            return this.http.get('/tenants/' + tenant + '/accounts/' + user.id + '/')
                .map((res: Response) => {
                    return res.json();
                });
        });
}

UserService (depends on external system, works as required):

getUser(): Promise<User> {
    return new Promise<User>((resolve, reject) => {
        if (KeycloakService.auth.authz.token) {
            return KeycloakService.auth.authz.loadUserInfo().success((data) => {
                resolve(<User> data);
            }).error(() => {
                reject(null);
            });
        }
    });
}

When calling to getAccount from AppComponent.ts:

this.accountService.getAccount().subscribe(data => { console.log('account:', data); });

I'm getting the following error:

EXCEPTION: Uncaught (in promise): Error: Error in :0:0 caused by: Cannot read property 'subscribe' of undefined TypeError: Cannot read property 'subscribe' of undefined

any ideas what causes this error?

thanks

Upvotes: 2

Views: 2008

Answers (1)

G&#252;nter Z&#246;chbauer
G&#252;nter Z&#246;chbauer

Reputation: 658067

A return is missing

return Observable.fromPromise(this.user

Upvotes: 3

Related Questions