![](/img/trans.png)
[英]TypeError: You provided 'undefined' where a stream was expected. You can provide an Observable, Promise, Array, or Iterable. at <Jasmine>
[英]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:您在需要流的地方提供了“未定義”。 您可以在 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:169248) at Observable._trySubscribe (vendor.js:169772) .subscribe (vendor.js:169758) 在 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 語句。 添加return next.handle(req.clone());
當localStorage.getItem('currentUser')
為 null 時在 else 塊中
Rishabh,其他人確實將此錯誤與“攔截器”的存在聯系起來,這很可能是這種情況。 但是,今天我得到了完全相同的 TypeError。 我的應用程序目前仍在開發中,不使用“攔截器”(可能在未來誰知道)。 它是一個 Angular 應用程序,當前與 API(函數 firebase)建立數據連接(版本 9.0.2)。 我正在實施一個新的組件(crud)'contractors',我決定在后端存儲數據之前使用'angular-in-memory-web-api'
一旦我在 App.Module 的“導入”部分實現了模塊“InMemoryWebApiModule.forRoot(ContractorData)”,我注意到在檢索另一個實體時突然無法訪問瀏覽器控制台中的 API:
錯誤類型錯誤:您在需要流的地方提供了“未定義”。 * 您可以提供 Observable、Promise、Array 或 Iterable。
因此,我的假設是:實現 ImMemoryWebApiModule 以這種方式干擾(在我的情況下)HttpClient 進程,它刪除或刪除有關發送到 api 的請求的信息。
希望這對您調查問題有所幫助!
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.