klmuralimohan
klmuralimohan

Reputation: 931

Angular 4: Expected 0 type arguments, but got 1

I'm trying to access and print the particular data in console window from the server. For some reason it throws an error on like

Expected 0 type arguments, but got 1

and the issue causes of get<ProductData> Can you please correct me what's wrong?

Note: If I access complete data without typed response it works fine.

interface ProductData {
  userId: string;
  id: number;
  title: string;    
} 


private productUrl:string = "https://jsonplaceholder.typicode.com/todos/1";

  ngOnInit() {

    this.http.get<ProductData>(this.productUrl).subscribe(data => {
          console.log(data.userId);
        })

}

Upvotes: 2

Views: 2601

Answers (1)

Gourishankar
Gourishankar

Reputation: 956

HttpClient has generic methods that can be used to provide response type. Http doesn't.

The error means that generic parameter wasn't expected, and http isn't an instance of HttpClient; likely an instance of Http.

If the application uses Angular 4.3 or higher, Http should be replaced with HttpClient. In case Http should be used, a response should be transformed, this is one of few differences between HttpClient and Http:

return this.http.get(uri, { params })
.map(res => <ProductData[]>res.json());

Details: https://angular.io/guide/http

Upvotes: 3

Related Questions