错误:referenceError:ErrorEvent未定义

时间:2019-04-18 00:32:00

标签: angular nativescript

当我尝试从json-server中获取数据时,出现此错误。
baseURL已定义并且可以通过例如我的浏览器访问,因此在这一点上没有问题。

欢迎提出任何建议。

dish.service.ts中的代码

import { Injectable } from '@angular/core';
import { Dish } from '../shared/dish';

import { Observable } from 'rxjs';
import { map, catchError } from 'rxjs/operators';
import { HttpClient, HttpHeaders } from '@angular/common/http';
import { baseURL } from '../shared/baseURL';
import { ProcessHTTPMsgService } from './process-httpmsg.service';

@Injectable({
  providedIn: 'root'
})
export class DishService {

  constructor(private http: HttpClient,
    private processHTTPMsgService: ProcessHTTPMsgService) { }

  getDishes(): Observable<Dish[]> {
    return this.http.get<Dish[]>(baseURL + 'dishes')
      .pipe(catchError(this.processHTTPMsgService.handleError));
  }

  getDish(id: string): Observable<Dish> {
    return this.http.get<Dish>(baseURL + 'dishes/' + id)
      .pipe(catchError(this.processHTTPMsgService.handleError));
  }

  getFeaturedDish(): Observable<Dish> {
    return this.http.get<Dish[]>(baseURL + 'dishes?featured=true').pipe(map(dishes => dishes[0]))
      .pipe(catchError(this.processHTTPMsgService.handleError));

在出现错误的情况下正在调用process-httpmsg.service.ts

import { Injectable } from '@angular/core';

import { throwError } from 'rxjs';
import { HttpErrorResponse } from '@angular/common/http';

@Injectable({
  providedIn: 'root'
})
export class ProcessHTTPMsgService {

  constructor() { }

  public handleError(error: HttpErrorResponse | any) {
    let errMsg: string;

    if (error.error instanceof ErrorEvent) {
      errMsg = error.error.message;
      console.log('errMsg (is instanceof ErrorEvent): ', errMsg)
    } else {
      errMsg = `${error.status} - ${error.statusText || ''} ${error.message}`;
      console.log('errMsg (is NOT instanceof ErrorEvent): ', errMsg)
    }

    return throwError(errMsg);
  }
}

1 个答案:

答案 0 :(得分:0)

不是答案,但是此信息可能会有所帮助。

我遇到了类似的问题,IE9中的ErrorEvent参考错误。

经调查,我发现ErrorEvent是IE9不支持的API(Javascript)。

ErrorEvent API:https://developer.mozilla.org/en-US/docs/Web/API/ErrorEvent

我正在这里寻找答案 ErrorEvent is not working for IE9 in Angular 8