0

I try to create a delete method but it doesn't work, I use json-server with reactive forms, my description of the problem is quite simple because what I ask is quite simple I'm a junior trying to figure out how it all works.

i have an error : ERROR TypeError: product is undefined

ts.file

export class ProduitsComponent implements OnInit {

  public dataSource: MatTableDataSource<Produit>;

  constructor(private produitService: ProduitService, public dialog: MatDialog) { }

  ngOnInit() {
    this.getLatestProduct();
  }

  createNewProduct(product: any) {
    this.produitService.createProduct(product).subscribe((response: Produit) => {
      let t = this.dataSource.data;
      t.push(response);
      this.dataSource.data = t;
    });
  }

  updateLatestProduct(product: Produit) {
    this.produitService.updateProduct(product).subscribe(response => {
      let index = this.dataSource.data.findIndex((value) => {value.id === product.id});
      this.dataSource.data.splice(index, 1, response);
    });
  }

  getLatestProduct() {
    let resp = this.produitService.getAllProduct();
    resp.subscribe(result => {
      this.dataSource = new MatTableDataSource<Produit>();
      this.dataSource.data = result as Produit[];
    });
  }

  delete(product: Produit) {
    this.produitService.deleteProduct(product).subscribe(() => {
      this.dataSource.data.filter((value, key) => {
        return value.id != product.id;
      });
    });
  }

service

 public deleteProduct(product: Produit) {
    return this.httpClient.delete(`${this.urlProductApi}produits/${product.id}`);
  }

html

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

   ......

    <!-- Comment Column -->
    <ng-container matColumnDef="comment">
        <th class="text" mat-header-cell *matHeaderCellDef>Commentaire</th>
        <td class="text2" mat-cell *matCellDef="let element"> {{element.comment}} </td>
    </ng-container>


    <!-- Action Column-->
    <ng-container matColumnDef="actions">
        <th mat-header-cell *matHeaderCellDef> Action </th>
        <td mat-cell *matCellDef="let row">
            <button mat-raised-button color="primary" (click)="editFormDialog(row)">Edit</button>
            <button mat-raised-button color="warn" (click)="delete()">Supprimer</button>
        </td>
        </ng-container>

    <tr mat-header-row *matHeaderRowDef="displayedColumns"></tr>
    <tr mat-row *matRowDef="let row; columns: displayedColumns;"></tr>

</table>

1 Answer 1

1

Modify delete() by adding row object as parameter in template like below to get the corresponding delete object

<button mat-raised-button color="warn" (click)="delete(row)">Supprimer</button>
Sign up to request clarification or add additional context in comments.

4 Comments

I have this error :​ message: "Http failure response for localhost:3000/produits/1: 404 Not Found"
Please check the rest url, seems there is typo.. ('products')
I don't see any typo
then this delete api call might not exist in server side, check the server code to confirm and do appropriate changes..

Your Answer

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