gpt4 book ai didi

angular - TypeError : You provided an invalid object where a stream was expected. 您可以提供一个 Observable、Promise、Array 或 Iterable

转载 作者:太空狗 更新时间:2023-10-29 16:47:58 24 4
gpt4 key购买 nike

我正在尝试通过服务调用映射,但出现错误。看了subscribe is not defined in angular 2?它说为了订阅我们需要从运营商内部返回。我也有返回声明。

这是我的代码:

checkLogin(): Observable<boolean> {
return this.service
.getData()
.map(
(response) => {
this.data = response;
this.checkservice = true;
return true;
},
(error) => {
// debugger;
this.router.navigate(["newpage"]);
console.log(error);
return false;
}
)
.catch((e) => {
return e;
});
}

错误日志:

TypeError: You provided an invalid object where a stream was expected. You can provide an Observable, Promise, Array, or Iterable

最佳答案

在我的案例中,错误仅发生在端到端测试期间。这是由我的 AuthenticationInterceptor 中的 throwError 引起的。

我从错误的来源导入它,因为我使用了 WebStorm 的导入功能。我正在使用 RxJS 6.2。

错误:

import { throwError } from 'rxjs/internal/observable/throwError';

正确:

import { throwError } from 'rxjs';

这里是拦截器的完整代码:

import { Injectable } from '@angular/core';
import { HttpErrorResponse, HttpEvent, HttpHandler, HttpInterceptor, HttpRequest } from '@angular/common/http';
import { Observable, throwError } from 'rxjs';
import { catchError } from 'rxjs/operators';

@Injectable()
export class AuthenticationInterceptor implements HttpInterceptor {

intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
const reqWithCredentials = req.clone({withCredentials: true});
return next.handle(reqWithCredentials)
.pipe(
catchError(error => {
if (error.status === 401 || error.status === 403) {
// handle error
}
return throwError(error);
})
);
}
}

关于angular - TypeError : You provided an invalid object where a stream was expected. 您可以提供一个 Observable、Promise、Array 或 Iterable,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43549223/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com