0
votes

I am trying to call an API from angular but facing an issue. Here is my code.

My Code :

import { Injectable } from "@angular/core";
import { Http } from "@angular/http";
import "rxjs/add/operator/map";
// import 'rxjs/add/observable/of';

@Injectable({
  providedIn: "root"
})
export class DataService {
  constructor(public http: Http) {
    console.log("data service connected");
  }

  getPosts() {
    return this.http
      .get("https://jsonplaceholder.typicode.com/posts")
      .map(res => res.json());
  }
}

I am facing an error saying "ts property 'map' does not exist on type 'observable response '"

1
why do you don't forget the old deprecated http and use httpClient? What about Rxjs 6? - Eliseo

1 Answers

0
votes

To solve this you must make the following import in your .ts file

import { map } from 'rxjs/operators/'

Then, in your function

return this.http.get("https://jsonplaceholder.typicode.com/posts").pipe(map(res => res.json()))

I hope help you. remember to rate if it served you. Good luck!