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

0 Answered Questions

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

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

1 Answered Questions

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

1 Answered Questions

PrimeNg <component> is not a known element error (@angular-2.0.0 & primeng-1.0.0-beta.16)

  • 2016-09-19 20:22:36
  • user6850401
  • 7661 View
  • 17 Score
  • 1 Answer
  • Tags:   angular primeng

0 Answered Questions

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

1 Answered Questions

3 Answered Questions

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

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

1 Answered Questions

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

2 Answered Questions

Detect change in each object of an array. Typescript/Angular 2

1 Answered Questions

[SOLVED] Issue with rendering Angularjs 2 PrimeNG Schedule component

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

Sponsored Content