Home > Net >  Angular 12 Material table. [dataSource] error: Provided data source did not match an array, Observab
Angular 12 Material table. [dataSource] error: Provided data source did not match an array, Observab

Time:12-01

My rest Api return { "id": 1, "userId": 1, "date": "2020-03-02T00:00:02.000Z", "products": [ { "productId": 1, "quantity": 4 }, { "productId": 2, "quantity": 1 }, { "productId": 3, "quantity": 6 } ], "__v": 0 } and I try to implement a Angular Material Table that show a cart of user selected by other table. I create a material module, service and interface... Can anybody help me understand what is causing the error here? I always get this error in the console: Provided data source did not match an array, Observable, or DataSource.

import { Component, OnInit } from '@angular/core';
import { ActivatedRoute } from '@angular/router';
import { Cart } from '../interfaces/cart';
import { CartsService } from '../services/carts.service';


@Component({
  selector: 'app-user-cart',
  templateUrl: './user-cart.component.html',
  styleUrls: ['./user-cart.component.css']
})
export class UserCartComponent implements OnInit {

  

  carts!: Cart[];
  
  displayedColumns: string[] = ['id','userId', 'date',  'products-id', 'products-quantity'];

  constructor(
    private cartsService: CartsService,
    private route: ActivatedRoute
  ) { }

  ngOnInit(): void {
    this.route.params.subscribe(response => this.cartsService.getCart(response.id)
    .subscribe(response => this.carts = response)
    );
  
  }

  

}

  
 <table mat-table [dataSource]="carts" class="mat-elevation-z8">

  <ng-container matColumnDef="id">
      <th mat-header-cell *matHeaderCellDef> Id </th>
      <td mat-cell *matCellDef="let cart"> {{cart.id}} </td>
    </ng-container> 

    <ng-container matColumnDef="userId">
        <th mat-header-cell *matHeaderCellDef> User Id </th>
        <td mat-cell *matCellDef="let cart"> {{cart.userId}} </td>
      </ng-container>
      
      <ng-container matColumnDef="date">
        <th mat-header-cell *matHeaderCellDef> Date </th>
        <td mat-cell *matCellDef="let cart"> {{cart.date}} </td>
      </ng-container> 

     <ng-container matColumnDef="products-id">
      <th mat-header-cell *matHeaderCellDef> Product Id </th>
      <td mat-cell *matCellDef="let row">
          <span *ngFor="let item of row.products">{{item.productId}}</span>
      </td>
    </ng-container>

    <ng-container matColumnDef="products-quantity">
        <th mat-header-cell *matHeaderCellDef> Quantity </th>
        <td mat-cell *matCellDef="let row">
            <span *ngFor="let item of row.products">{{item.quantity}}</span>
        </td>
      </ng-container> 
  
 <tr mat-header-row *matHeaderRowDef="displayedColumns"></tr>
    <tr mat-row *matRowDef="let row; columns: displayedColumns;">
    </tr>
  </table>
<iframe name="sif1" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>

export interface Cart {
    id: number,
    userId: number,
    date: string,
    products: Array<{productId: number, quantity: number}>,
    __v: number 
}
<iframe name="sif2" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>

CodePudding user response:

Not sure if that can solve your problem, but I don't think a list is required on your datasource.

I would suggest you try replacing:

carts!: Cart[];

By :

public carts: MatTableDataSource<Cart>;

with the related import of course: import { MatTableDataSource } from '@angular/material/table';

CodePudding user response:

Initialize the data source cart as below

carts = MatTableDataSource<Cart[]>;

import { MatTableDataSource } from '@angular/material/table';

  • Related