Angular RxJS Poll实时数据switchMap类型错误
Angular RxJS Poll real-time data switchMap type error
我正在尝试使用 interval
和 switchMap
从我的服务器使用 RxJS 轮询实时数据。当用户导航到组件时,它将触发轮询。我通过使用以下服务代码直接请求 HTTP 端点(而不是扩展基础 class)来使其工作。
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);
})
);
}
但是,我无法理解为什么当我扩展基本服务 class 并在基本服务中做同样的事情时 class 它给我一个错误
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)
您忘记了请求流的 return。
switchMap(() => {
return this.requestURLBase(); // <- return!!!
}),
我正在尝试使用 interval
和 switchMap
从我的服务器使用 RxJS 轮询实时数据。当用户导航到组件时,它将触发轮询。我通过使用以下服务代码直接请求 HTTP 端点(而不是扩展基础 class)来使其工作。
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);
})
);
}
但是,我无法理解为什么当我扩展基本服务 class 并在基本服务中做同样的事情时 class 它给我一个错误
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)
您忘记了请求流的 return。
switchMap(() => {
return this.requestURLBase(); // <- return!!!
}),