Reputation: 221
I have service file which is returning a subscribable result on some condition(obviously making an http call) and returning a normal object on some other conditions. Since I have used .subscribe
in component.ts
for calling this service method, getting "TypeError: Object doesn't support property or method 'map'" on condition where http calls are not happening.
component.ts
file code
this.myservice.userAuthenticate( 'user' ).subscribe(result => {
console.log('results', result);
});
and my service file is like this
public userAuthenticate(user ): Observable<any> {
const expired = this.expiry.hasExpired();
if ( expired )
return this.userService.generateUserId('some input' ).map( data => {
this.CheckUser( User);
return <any>userInfo;
} );
}
} else {
return this.CheckUser( User).map( response => {
return <any>userInfo;
} );
}
In the else part, the Im just getting data from storage and processing it using CheckUser
method. There is no http calls are happening and it is giving me the above error. Is there any way I can return a subscribable result even without a http calls ?
One work around is to have this check from component file and only subscribe for first part, but I have to make the same changes in lot of places as this particular method in many places. Could anyone please let me know how can I give a subscribable response in else condition ?
Upvotes: 1
Views: 2267
Reputation: 96959
So the problem is that this.CheckUser(User)
isn't returning an Observable.
I'm not completely sure what your code does and why you're this.CheckUser(User)
chaining with map
while above you're not which is kind of confusing but still you can wrap the method call with Observable.of()
:
return Observable.of(this.CheckUser(User)).map(response => {
return <any>userInfo;
});
Eventually you could use Observable.defer(() => this.CheckUser(User))
if you want the callback to be invoked only after you subscribe to this chain.
Upvotes: 1