所以有一些奇怪的事情发生了,我正在制作一个HTTP服务但是我收到了一个错误,奇怪的是它有时会起作用,有时却没有,但是在过去的24小时里它已经不再有用了 . 服务电话和订阅的代码如下 . 然后还有错误 .

category.service.ts

getcategory(id) {
const url = `${this.localUrl}category/${id}`;
  return this.http.get<CategoryResponse>(url)
    .pipe(
      catchError(this.errorHandler)
    )

}

错误处理程序

errorHandler(error: HttpErrorResponse) {
  if (error.error instanceof ErrorEvent) {
  // A client-side or network error occurred. Handle it accordingly.
  console.error('An error occurred:', error.error.message);
} else {
  // The backend returned an unsuccessful response code.
  console.error(
    `Backend returned code ${error.status}, ` +
    `body was: ${error.error}`);
}

    // return an observable with a user-facing error message
   return throwError(
    'Something bad happened; please try again later.');
}

附加编辑category.component.ts

if (this.router.url.indexOf('edit') >= 0) {
      this.route.paramMap.pipe(switchMap((params: ParamMap) => 
        this.categoryService.getcategory(params.get('id'))))
        .subscribe(results => {
           this.category = results.category;

          this.categoryNameControl.setValue(this.category.categoryName);
          this.categoryDescriptionControl.setValue(this.category.categoryDescription);
          console.log("I am edit", Object.keys(config))
          console.log("TEst again",this.category)
        }, error => {
            console.log("Error",error)
        });
     }

错误

Error TypeError: You provided an invalid object where a stream was expected. You can provide an Observable, Promise, Array, or Iterable.
at Object.push../node_modules/rxjs/internal/util/subscribeTo.js.exports.subscribeTo (subscribeTo.js:42)
at Object.subscribeToResult (subscribeToResult.js:7)
at SwitchMapSubscriber.push../node_modules/rxjs/internal/operators/switchMap.js.SwitchMapSubscriber._innerSub (switchMap.js:103)
at SwitchMapSubscriber.push../node_modules/rxjs/internal/operators/switchMap.js.SwitchMapSubscriber._next (switchMap.js:96)
at SwitchMapSubscriber.push../node_modules/rxjs/internal/Subscriber.js.Subscriber.next (Subscriber.js:103)
at MapSubscriber.push../node_modules/rxjs/_esm5/internal/operators/map.js.MapSubscriber._next (map.js:81)
at MapSubscriber.push../node_modules/rxjs/_esm5/internal/Subscriber.js.Subscriber.next (Subscriber.js:93)
at BehaviorSubject.push../node_modules/rxjs/_esm5/internal/BehaviorSubject.js.BehaviorSubject._subscribe (BehaviorSubject.js:26)
at BehaviorSubject.push../node_modules/rxjs/_esm5/internal/Observable.js.Observable._trySubscribe (Observable.js:176)
at BehaviorSubject.push../node_modules/rxjs/_esm5/internal/Subject.js.Subject._trySubscribe (Subject.js:96)