Типы параметров «источник» и «источник» несовместимы - PullRequest
0 голосов
/ 05 мая 2020

Я пытаюсь получить доступ к данным из файла foo. json, используя следующую функцию getFoo.

    getFoo(): Observable<IFoo[]> {
      return this.http.get<IFoo[]>(this.fooUrl).pipe(
      tap(data => console.log('All: ' + JSON.stringify(data))),
      catchError(this.handleError)
      );
   }

Я возвращаю ошибку:

     Argument of type 'UnaryFunction<Observable<IFoo[]>, Observable<IFoo[]>>' is 
     not assignable to parameter of type 'OperatorFunction<IFoo[], IFoo[]>'.
     Types of parameters 'source' and 'source' are incompatible.
     Type 'Observable<IFoo[]>' is missing the following properties from type 
    'Observable<IFoo[]>': buffer, bufferCount, bufferTime, bufferToggle, and 104 more.

Не совсем уверен, как это исправить.

Выполняется angular 9.0.3, машинописный текст 3.7.5.

ОБНОВЛЕНИЕ:

Это моя ошибка handleError:

private handleError(err: HttpErrorResponse) {

  let errorMessage = '';
  if (err.error instanceof ErrorEvent) {
    errorMessage = `An error occurred: ${err.error.message}`;
  } else {

    errorMessage = `Server returned code: ${err.status}, error message is: ${err.message}`;
  }
  console.error(errorMessage);
  return _throw (errorMessage);
}

Ответы [ 2 ]

0 голосов
/ 05 мая 2020

Поскольку вы используете одну из последних версий Angular, она должна использовать Rx JS 6+. _throw был заменен на throwError:

private handleError(err: HttpErrorResponse) {

  let errorMessage = '';
  if (err.error instanceof ErrorEvent) {
    errorMessage = `An error occurred: ${err.error.message}`;
  } else {
    errorMessage = `Server returned code: ${err.status}, error message is: 
   ${err.message}`;
  }
  return throwError(errorMessage);
}

Если вышеуказанное по-прежнему не решает проблему, вам необходимо исправить getFoo так, чтобы все пути фактически возвращали наблюдаемый тип IFoo[], поскольку он объявлен как возвращаемый тип getFoo

getFoo(): Observable<IFoo[] | HttpErrorResponse> {
  return this.http.get<IFoo[]>(this.fooUrl).pipe(
  map(data => {
    console.log('All: ' + JSON.stringify(data));
    return data;
  }),
  catchError(this.handleError)
  );
}
0 голосов
/ 05 мая 2020

Это должна быть правильная версия (без обобщений в get и с функцией карты):

import { map } from 'rxjs/operators';
...

getFoo(): Observable<IFoo[]> {
  return this.http.get(this.fooUrl).pipe(
  // tap(data => console.log('All: ' + JSON.stringify(data))),
  map<any, IFoo[]>(data => {
    console.log('All: ' + JSON.stringify(data)); 
    return data;
  }),
  catchError(this.handleError)
  );
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...