Error: "You provided 'undefined' where a stream was expected. You can provide an Observable, Promise, Array, or Iterable." in my Auth Interceptor
Error: "You provided 'undefined' where a stream was expected. You can provide an Observable, Promise, Array, or Iterable." in my Auth Interceptor
我想拦截对 api 的每个请求并检查状态代码,以便它可以显示消息或重定向到特定组件,但出现以下错误:
main.js:1580 TypeError: You provided 'undefined' where a stream was expected. You can provide an >Observable, Promise, Array, or Iterable.at subscribeTo (vendor.js:179688)at subscribeToResult(vendor.js:179824) at MergeMapSubscriber._innerSub (vendor.js:175271) at mergeMapSubscriber._tryNext (vendor.js:175265) at MergeMapSubscriber._next (vendor.js:175248)
at MergeMapSubscriber.next (vendor.js:170316) at Observable._subscribe (vendor.js:172287)
at Observable._trySubscribe (vendor.js:169772) at Observable.subscribe (vendor.js:169758)
at MergeMapOperator.call (vendor.js:175233)
这是我的 AuthInterceptor:
import { Injectable } from '@angular/core';
import { HttpRequest, HttpHandler, HttpEvent, HttpInterceptor, HttpHeaders, HttpErrorResponse } from '@angular/common/http';
import { Observable, throwError } from 'rxjs';
import { tap } from 'rxjs/operators';
import { CommonService } from '../common.service';
import { Router } from '@angular/router';
import { AuthenticationService } from './authentication.service';
@Injectable({
providedIn: 'root'
})
export class AuthInterceptorService implements HttpInterceptor {
constructor(
private common: CommonService,
private router: Router,
private auth: AuthenticationService
) { }
intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
if (req.headers.get('No-Auth') === 'True') {
return next.handle(req.clone());
}
// To attach header on every request
if (localStorage.getItem('currentUser') != null) {
const clonedreq = req.clone({
headers: req.headers.set('Authorization', 'Bearer ' + localStorage.getItem('currentUser'))
});
return next.handle(clonedreq).pipe(tap(
succ => { },
err => {
if (err.status === 401) {
this.router.navigateByUrl('/login');
} else if (err.status === 403) {
this.router.navigateByUrl('/Forbidden');
} else if (err.status === 400) {
this.router.navigateByUrl('/error404');
}
}
));
} else {
this.router.navigateByUrl('/login');
}
}
}
我无法找出导致它的行或块,因为它没有写在错误的任何地方。该项目编译得很好。
该错误几乎显示在向 WEB 发送请求的项目的每个页面中 Api.
在 else 块中,您缺少 return 语句。
添加
当 localStorage.getItem('currentUser')
为 null
时,return next.handle(req.clone());
在 else 块中
Rishabh,其他人 link 这个错误存在 'interceptors',很可能就是这种情况。然而,今天我得到了完全相同的 TypeError。我的应用程序目前仍在开发中,不使用 'interceptors'(将来可能会知道)。这是一个 Angular 应用程序,目前最新数据(版本 9.0.2)连接到 API(功能为 firebase)。我正在实施一个新组件 (crud) 'contractors',我决定在其上使用 'angular-in-memory-web-api' 之前在后端存储数据
当我在 App.Module 的 'imports' 部分实现模块 'InMemoryWebApiModule.forRoot(ContractorData)' 后,我注意到在检索另一个实体时我突然无法访问 API 在浏览器控制台中注意到:
错误类型错误:您提供了 'undefined' 预期流的位置。 * 您可以提供 Observable、Promise、Array 或 Iterable。
所以,我的假设是:实施 ImMemoryWebApiModule 以这种方式干扰(在我的例子中)HttpClient 进程,它删除或删除发送到 api 的请求的信息。
见截图
希望这对您调查问题有所帮助!
我想拦截对 api 的每个请求并检查状态代码,以便它可以显示消息或重定向到特定组件,但出现以下错误:
main.js:1580 TypeError: You provided 'undefined' where a stream was expected. You can provide an >Observable, Promise, Array, or Iterable.at subscribeTo (vendor.js:179688)at subscribeToResult(vendor.js:179824) at MergeMapSubscriber._innerSub (vendor.js:175271) at mergeMapSubscriber._tryNext (vendor.js:175265) at MergeMapSubscriber._next (vendor.js:175248) at MergeMapSubscriber.next (vendor.js:170316) at Observable._subscribe (vendor.js:172287) at Observable._trySubscribe (vendor.js:169772) at Observable.subscribe (vendor.js:169758) at MergeMapOperator.call (vendor.js:175233)
这是我的 AuthInterceptor:
import { Injectable } from '@angular/core';
import { HttpRequest, HttpHandler, HttpEvent, HttpInterceptor, HttpHeaders, HttpErrorResponse } from '@angular/common/http';
import { Observable, throwError } from 'rxjs';
import { tap } from 'rxjs/operators';
import { CommonService } from '../common.service';
import { Router } from '@angular/router';
import { AuthenticationService } from './authentication.service';
@Injectable({
providedIn: 'root'
})
export class AuthInterceptorService implements HttpInterceptor {
constructor(
private common: CommonService,
private router: Router,
private auth: AuthenticationService
) { }
intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
if (req.headers.get('No-Auth') === 'True') {
return next.handle(req.clone());
}
// To attach header on every request
if (localStorage.getItem('currentUser') != null) {
const clonedreq = req.clone({
headers: req.headers.set('Authorization', 'Bearer ' + localStorage.getItem('currentUser'))
});
return next.handle(clonedreq).pipe(tap(
succ => { },
err => {
if (err.status === 401) {
this.router.navigateByUrl('/login');
} else if (err.status === 403) {
this.router.navigateByUrl('/Forbidden');
} else if (err.status === 400) {
this.router.navigateByUrl('/error404');
}
}
));
} else {
this.router.navigateByUrl('/login');
}
}
}
我无法找出导致它的行或块,因为它没有写在错误的任何地方。该项目编译得很好。 该错误几乎显示在向 WEB 发送请求的项目的每个页面中 Api.
在 else 块中,您缺少 return 语句。
添加
当 localStorage.getItem('currentUser')
为 null
return next.handle(req.clone());
在 else 块中
Rishabh,其他人 link 这个错误存在 'interceptors',很可能就是这种情况。然而,今天我得到了完全相同的 TypeError。我的应用程序目前仍在开发中,不使用 'interceptors'(将来可能会知道)。这是一个 Angular 应用程序,目前最新数据(版本 9.0.2)连接到 API(功能为 firebase)。我正在实施一个新组件 (crud) 'contractors',我决定在其上使用 'angular-in-memory-web-api' 之前在后端存储数据
当我在 App.Module 的 'imports' 部分实现模块 'InMemoryWebApiModule.forRoot(ContractorData)' 后,我注意到在检索另一个实体时我突然无法访问 API 在浏览器控制台中注意到:
错误类型错误:您提供了 'undefined' 预期流的位置。 * 您可以提供 Observable、Promise、Array 或 Iterable。
所以,我的假设是:实施 ImMemoryWebApiModule 以这种方式干扰(在我的例子中)HttpClient 进程,它删除或删除发送到 api 的请求的信息。
见截图
希望这对您调查问题有所帮助!