删除 http://localhost:3001/formations/undefined 0 ()

DELETE http://localhost:3001/formations/undefined 0 ()

删除功能不再有效。

我从 Delete HTTP 调用中删除了我的信息,它可以工作,但是当我在 angular2 部分前端中使用时,它不再工作了。

错误 删除 http://localhost:3001/formations/undefined 0 ()

异常:状态响应:0 for URL:null

Subscriber.js:246 未捕获响应 {_body: ProgressEvent, status: 0, ok: false, statusText: "", headers: Headers, ...}

zone.js:2019 删除 http://localhost:3001/formations/undefined 0 ()

这是我的formation.Service

deleteFormation(id){
    return this.http.delete("http://localhost:3001/formations/"+id)
        .map(res => res.json());
  }

这是我的home.ts

import { Component, OnInit } from '@angular/core';
import { FormationService } from '../../services/formation.service';
import { Formation } from '../../../app/formation';
import {Observable} from 'rxjs/Rx';



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

  formation: Observable<Formation[]>;


  constructor(  
      public formationService:FormationService

  ) { };

  ngOnInit() {
    this.formation = this.formationService.getFormations();
   // this.getFormations();
  }

  getFormations(){
    this.formationService.getFormations()
        .subscribe(formation=>{
          this.formation = this.formation;
        })


}

deleteFormation(id) {
  this.formationService.deleteFormation(id)
    .subscribe(()=>{
      this.getFormations();
    });
}
}

这是我的home.html

<app-navbar1></app-navbar1>



<table class="table table-bordered">
  <thead>
    <tr>
      <td><b>Title</b></td>
      <td><b>url</b></td>
      <td><b>description</b></td>
      <td width="275" align="center"><b>Action</b></td>
    </tr>
  </thead>
  <tbody>  
     <tr *ngFor="let forms of formation  | async " >
        <td>{{forms.title}}</td>
        <td>{{forms.url}}</td> 
        <td>{{forms.description}}</td>
        <td width="275"> 
            <a class="btn btn-info" routerLink="/show/{{formation._id}}">Detail</a> 
            <a class="btn btn-success" routerLink="/edit/{{formation._id}}" >Edit</a>
            <a class="btn btn-danger" (click)="deleteFormation(formation._id)" >Delete</a>
        </td>
        </tr>


  </tbody>
</table>

您的代码存在的问题是您在 *ngFor 中枚举集合,而不是将对象的实例传递给 deleteFormation 方法,而是传递集合。

您需要调用 deleteFormation(forms._id) 而不是 deleteFormation(formation._id)_id 在您的 formation 数组中不存在。

同样的问题适用于您的 DetailEdit 链接。