【发布时间】:2020-04-18 23:33:08
【问题描述】:
我正在尝试使用 interval 和 switchMap 使用 RxJS 从我的服务器轮询实时数据。当用户导航到该组件时,它将触发投票。我通过使用以下服务代码直接请求 HTTP 端点(而不是扩展基类)来使其工作。
myService.ts:
public getRealTimeData() {
return interval(5000).pipe(
switchMap(() => {
return this.httpClient.get('http://myendpoint:3000/endpoint');
}),
catchError((err) => {
console.log('Handling error locally and rethrowing it...', err);
return throwError(err);
})
);
}
但是,我无法理解为什么当我扩展一个基服务类并在基类中做同样的事情时它会给我一个错误
myService.ts:
...
constructor(){
super(httpClient, 'my_custom_route')
}
public getRealTimeDataExtendingBaseClass() {
return interval(5000).pipe(
switchMap(() => {
this.requestURLBase();
}),
catchError((err) => {
console.log('Handling error locally and rethrowing it...', err);
return throwError(err);
})
);
}
baseService.ts:
protected baseUrlPrefix = 'api/v1/';
constructor(protected httpClient: HttpClient, route: string) {
this.route = route;
}
...
public requestURLBase() {
return this.httpClient.get(this.baseUrlPrefix + this.url);
}
这是错误堆栈:
errors.ts:30 ERROR TypeError: You provided 'undefined' where a stream was expected. You can provide an Observable, Promise, Array, or Iterable.
at Object.exports.subscribeTo (subscribeTo.ts:27)
at Object.subscribeToResult (subscribeToResult.ts:36)
at SwitchMapSubscriber._innerSub (switchMap.ts:139)
at SwitchMapSubscriber._next (switchMap.ts:128)
at SwitchMapSubscriber.Subscriber.next (Subscriber.ts:99)
at AsyncAction.dispatch (interval.ts:75)
at AsyncAction._execute (AsyncAction.ts:122)
at AsyncAction.execute (AsyncAction.ts:97)
at AsyncScheduler.flush (AsyncScheduler.ts:58)
at ZoneDelegate.invokeTask (zone.js:421)
【问题讨论】:
-
它是 switchmap 中的大括号。删除这些或使用 return
this.requestURLBase()。
标签: angular rxjs httpclient