【问题标题】:DELETE http://localhost:3001/formations/undefined 0 ()删除 http://localhost:3001/formations/undefined 0 ()
【发布时间】:2019-01-07 16:57:28
【问题描述】:

删除功能不再起作用。

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

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

例外:响应状态:0 对于 URL:null

Subscriber.js:246 Uncaught Response {_body: ProgressEvent, status: 0, ok: false, statusText: "", headers: Headers, ...}

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

这是我的队形。服务

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

这是我的家。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();
    });
}
}

这是我的家.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>

【问题讨论】:

  • 不知何故,您的 id 未定义。检查组件中调用deleteFormation方法的视图是否传递了正确的id。

标签: node.js angular angular2-routing


【解决方案1】:

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

您需要拨打deleteFormation(forms._id),而不是deleteFormation(formation._id)_id 不存在于您的 formation 数组中。

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

【讨论】:

  • 谢谢,它可以工作,但是,为什么我点击deleteFunction时需要刷新页面仍然是一个小问题
猜你喜欢
  • 1970-01-01
  • 2021-12-17
  • 1970-01-01
  • 2021-12-12
  • 2015-12-04
  • 1970-01-01
  • 2023-02-16
  • 1970-01-01
  • 2023-01-26
相关资源
最近更新 更多