By FakeEmpire


2017-11-27 15:01:28 8 Comments

I am using PrimeNG datatable. I using httpClient in Angular to fetch some mock data from JSON Placeholder. It appears in my console as an array of objects, however the Visual Studio Code error is saying it is an object. The error says 'type Object is not assignable to any[].' What is the problem here?

table-layout.component.ts

import { BrowserModule } from '@angular/platform-browser'
import { Component, OnInit, NgModule } from '@angular/core';
import { HttpClient } from '@angular/common/http'

@Component({
  selector: 'app-table-layout',
  templateUrl: './table-layout.component.html',
  styleUrls: ['./table-layout.component.css']
})

export class TableLayoutComponent implements OnInit {

  ROOT_URL: string = 'https://jsonplaceholder.typicode.com/users'
  results: any[]

  constructor(private http: HttpClient) { }

  ngOnInit() {
    this.getData();
  }

  getData() {
    this.http.get(this.ROOT_URL).subscribe(data => {
      this.results = data
      console.log(this.results) //this is an array in the console 
    })
  }

}

table-layout.component.html

<p-dataTable [value]="results">
</p-dataTable>

1 comments

@LLai 2017-11-27 15:05:11

If you do not specify the type that is returned from your http request, the http client assumes its an Object. This is causing the type mismatch error that you are seeing. You are trying to assign type Object to type any[]. You can specify the return type by doing

this.http.get<any[]>(this.ROOT_URL).subscribe(...);

@FakeEmpire 2017-11-27 15:18:21

Thank you so much!

Related Questions

Sponsored Content

2 Answered Questions

Angular http request

  • 2018-06-19 11:18:02
  • Shiva
  • 86 View
  • 0 Score
  • 2 Answer
  • Tags:   angular

2 Answered Questions

0 Answered Questions

Angular - ng prime datatable-how to add data in dialog

  • 2018-03-14 13:34:02
  • Satishchandra Pandey
  • 34 View
  • 0 Score
  • 0 Answer
  • Tags:   angular

1 Answered Questions

[SOLVED] Fetching data from REST Web Service using Angular 2 Http

0 Answered Questions

1 Answered Questions

0 Answered Questions

angular - Type 'Object' is not assignable to type 'any[]'

3 Answered Questions

[SOLVED] 'p-header' is not a known element in PrimeNg for Angular2 application

  • 2017-01-23 12:41:29
  • Rj-s
  • 12742 View
  • 7 Score
  • 3 Answer
  • Tags:   angular primeng

1 Answered Questions

[SOLVED] How to do dynamically row grouping on PrimeNG Datatable?

1 Answered Questions

[SOLVED] Issue with rendering Angularjs 2 PrimeNG Schedule component

  • 2017-03-03 01:18:41
  • Ramesh
  • 317 View
  • 0 Score
  • 1 Answer
  • Tags:   angular primeng

Sponsored Content