0

Im trying to connect backend data to my dataSource and I keep getting an error saying: ERROR

Error: Provided data source did not match an array, Observable, or DataSource
export class AdminComponent implements OnInit {

  dataSource: Object; 

  this.data.getUsers().subscribe(data => {
    this.dataSource = data;
  });
1

1 Answer 1

1

datesource type is wrong. don't specify Object. make sure its either : array, Observable, or DataSource. example:

import {Component} from '@angular/core';

export interface PeriodicElement {
  name: string;
  position: number;
  weight: number;
  symbol: string;
}

const ELEMENT_DATA: PeriodicElement[] = [
  {position: 1, name: 'Hydrogen', weight: 1.0079, symbol: 'H'},
  {position: 2, name: 'Helium', weight: 4.0026, symbol: 'He'},
  {position: 3, name: 'Lithium', weight: 6.941, symbol: 'Li'},
  {position: 4, name: 'Beryllium', weight: 9.0122, symbol: 'Be'},
  {position: 5, name: 'Boron', weight: 10.811, symbol: 'B'},
  {position: 6, name: 'Carbon', weight: 12.0107, symbol: 'C'},
  {position: 7, name: 'Nitrogen', weight: 14.0067, symbol: 'N'},
  {position: 8, name: 'Oxygen', weight: 15.9994, symbol: 'O'},
  {position: 9, name: 'Fluorine', weight: 18.9984, symbol: 'F'},
  {position: 10, name: 'Neon', weight: 20.1797, symbol: 'Ne'},
];

/**
 * @title Basic use of `<table mat-table>`
 */
@Component({
  selector: 'table-basic-example',
  styleUrls: ['table-basic-example.css'],
  templateUrl: 'table-basic-example.html',
})
export class TableBasicExample {
  displayedColumns: string[] = ['position', 'name', 'weight', 'symbol'];
  dataSource = ELEMENT_DATA;
}
Sign up to request clarification or add additional context in comments.

3 Comments

Yes I did it that way at first and it works fine. the problem with that way is I can't pass variables from my component to the data source. Like if I have data coming from a backend service
Other example with observables. The clue is that the datasource shouldnt be defined as an Object. blog.angular-university.io/angular-material-data-table and techiediaries.com/angular-material-table
i changed it to this but I don't think its the correct way of doing it: dataSource = []; this.data.getUsers().subscribe(data => { this.dataSource = [data]; console.log(this.dataSource); });

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.