【问题标题】:mongoose; 404 when remove object from collection with _id猫鼬;使用 _id 从集合中删除对象时出现 404
【发布时间】:2020-09-19 13:43:57
【问题描述】:

我对从集合中删除对象的 mongoose api 有一个奇怪的行为。

我调用了一个将 id 作为参数传递的 api(我检查了并且 ID 存在), 但我得到的是响应 404。

这里我如何使用 Angular 服务调用 api:

  private deleteInvoice = 'http://localhost:3000/api/elimina_fattura';    

 deleteInvoices(id){
   let headers = new Headers();
   headers.append('Content-Type', 'application/json');
   return this.http.post(this.deleteInvoice+'/'+id, {headers: headers})
   .map((response: Response) => response.json())    
 }

这里是mongoose中定义的api

app.delete('/api/elimina_fattura/:id',(req,res)=>{
Fatture.remove({_id: req.params.id})
.then(()=>{
    res.json({'status':'ok'});
 })
 .catch((err)=>{
    res.json(err);
});
});

这里是我调用 api 时的消息

我曾尝试在 robomongo 中执行相同的查询,并且似乎可以正常工作

【问题讨论】:

    标签: node.js angular mongodb mongoose


    【解决方案1】:

    我可以在前端看到您提到的请求类型 POST 尝试将其更改为 DELETE

     deleteInvoices(id){
       let headers = new Headers();
       headers.append('Content-Type', 'application/json');
       return this.http.delete(this.deleteInvoice+'/'+id, {headers: headers}) // changed HTTP method
       .map((response: Response) => response.json())    
     }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-10-23
      • 2015-06-07
      • 2017-03-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多