0

I'm new to Angular and while doing development the below code will not work (I'm using Visual Studio Code as the code editor)

post.service.ts file

import { Injectable } from '@angular/core';
import { Http } from '@angular/http';
import { Observable } from 'rxjs/Observable';
import  'rxjs/add/operator/catch';
import { AppError } from 'src/common/app-error';

@Injectable({
  providedIn: 'root'
})
export class PostService {
  private url = 'https://jsonplaceholder.typicode.com/posts';
  constructor(private http: Http) {

  }

deletePost(id) {
    return  this.http.delete(this.url + '/' + id)
    .catch((error:Response) => {
      Observable.throw(new AppError(error));

     });
  }

}

app-error.ts file

export class AppError {

    constructor(public originalError?: any) { }
}

3 errors are there

  • Property 'catch' does not exist on type 'Observable'
  • Cannot find module 'rxjs-compat/Observable'
  • Module '"/node_modules/rxjs/Observable"' has no exported member 'Observable'

I expect for a solutions from all experts

Sanjeewa
  • 555
  • 1
  • 9
  • 22
  • 3
    `.pipe(catchError` https://angular.io/guide/http#getting-error-details – yurzui Nov 17 '18 at 14:18
  • @Sanjeewa I have implemented your API with a working demo [here](https://stackblitz.com/edit/angular-dc25xo?file=app%2Fbutton-overview-example.ts) have a look at! --) – Prashant Pimpale Nov 17 '18 at 14:59
  • modified and now working fine `deletePost(id) { return this.http.delete(this.url + '/' + id) .pipe( catchError(error => { if (error.status == 404) { return throwError(new NotFoundError(error)); } else { return throwError(new AppError(error)); } }) ); }` – Sanjeewa Nov 17 '18 at 17:06

0 Answers0