I use Laravel 5.8 as backend and Angular7 as Front end. When I clicked on the Delete Submit button, nothing happens

I checked the console, I have this error:

SmsShortcodeMyComponent.html:85 ERROR TypeError: _co.deleteShortcode is not a function

SmsShortcodeMyComponent.html:84 ERROR TypeError: _co.updateShortcode is not a function

Laravel: ShortcodeController

    public function destroy(Shortcode $shortcode)
    {
        $shortcode->delete();

        return response()->json([
            'message' => 'Successfully deleted Short Code!'
        ]);
    }

    public function update(Request $request, Shortcode $shortcode)
    {
        $request->validate([
           'name'       => 'nullable'
        ]);

        $shortcode->update($request->all());

        return response()->json([
            'message' => 'Great success! Shortcode updated',
            'shortcode' => $shortcode
        ]);
    }

api.php

Route::middleware('auth:api')->get('/user', function (Request $request) {
    return $request->user();
});


Route::get('/shortcodes', '[email protected]')->name('shortcodes.index');

Route::post('/shortcodes', '[email protected]')->name('shortcodes.store');

Route::get('/shortcodes/{shortcode}', '[email protected]')->name('shortcodes.show');

Route::put('/shortcodes/{shortcode}', '[email protected]')->name('shortcodes.update');

Route::delete('/shortcodes/{shortcode}', '[email protected]')->name('shortcodes.destroy');

Angular7: shortcode.service.ts

  updateShortcode (id, shortcode): Observable<any> {
    const url = `${apiUrl}/${id}`;
    return this.http.put(url, shortcode, httpOptions).pipe(
      tap(_ => console.log(`updated shortcode id=${id}`)),
      catchError(this.handleError<any>('updateShortcode'))
    );
  }

  deleteShortcode (id): Observable<Shortcode> {
    const url = `${apiUrl}/${id}`;

    return this.http.delete<Shortcode>(url, httpOptions).pipe(
      tap(_ => console.log(`deleted shortcode id=${id}`)),
      catchError(this.handleError<Shortcode>('deleteShortcode'))
    );
  }

  private handleError<T> (operation = 'operation', result?: T) {
    return (error: any): Observable<T> => {

      // TODO: send the error to remote logging infrastructure
      console.error(error); // log to console instead

      // Let the app keep running by returning an empty result.
      return of(result as T);
    };
  }

sms-shortcode-my.component.html

              <div class="box-body">
                <table id="example2" class="table table-bordered table-hover table-striped table-condesed">
                  <thead>
                  <tr>
                    <th class="hidden">Id</th>
                    <th width="70%">Shortcode</th>
                    <th>Actions</th>                   
                  </tr>
                  </thead>
                  <tbody>
                  <tr  *ngFor="let datas of data">
                      <td>{{datas.name}}</td>
                    <td>
                        <button class="btn btn-info" (click)="updateShortcode(shortcode)">Edit</button>
                        <button class="btn btn-danger" (click)="deleteShortcode(shortcode)" style="margin-left: 20px;"> Delete</button>
                    </td>                                  
                  </tr>
                </table>
              </div>

typescript

import { Component, OnInit } from '@angular/core';
import { ShortcodeService } from '../../../services/shortcode.service';
import { Shortcode } from '../../../models/shortcode';

@Component({
  selector: 'app-sms-shortcode-my',
  templateUrl: './sms-shortcode-my.component.html',
  styleUrls: ['./sms-shortcode-my.component.scss']
})
export class SmsShortcodeMyComponent implements OnInit {
  displayedColumns: string[] = ['name'];
  data: Shortcode[] = [];
  isLoadingResults = true;  

  constructor(private api: ShortcodeService) { }
  ngOnInit() {

    this.api.getShortcodes()
      .subscribe(res => {
        this.data = res;
        console.log(this.data);
        this.isLoadingResults = false;
      }, err => {
        console.log(err);
        this.isLoadingResults = false;
      });
  }

  ngOnDestroy(): void {
    document.body.className = '';
  }

}

When I clicked on the delete button, I expected the row to be deleted from database but nothing happens.

Thanks

0 Answers