node.js 使用 findOneAndRemove Mongoose 删除文档

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22601365/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 17:03:43  来源:igfitidea点击:

Delete document using findOneAndRemove Mongoose

node.jsmongodbmongoosepugnode.js-connect

提问by user2175731

I am receiving this error when trying to delete a document from the database:

尝试从数据库中删除文档时收到此错误:

Cannot GET /delete/532fa5e56f885c7fec5223b1fds

无法获取 /delete/532fa5e56f885c7fec5223b1fds

How can I successfully delete the document?

如何成功删除文档?

app.js

应用程序.js

//Delete 
app.del('/delete/:id', routes.delete_offer);

routes/index.js

路线/ index.js

    //Delete
    exports.delete_offer = function (req,res){
      Offer.findOneAndRemove({'_id' : req.params.id}, function (err,offer){
        res.redirect('/newsfeed');
      });
    };

views/dashboard.jade

视图/dashboard.jade

        - each offer in offers
            div.offer.row
                a(href="/offer/" + offer._id)
                    div.columns
                        div.sell_type
                            p=offer.type
                    div.small-8.columns.sell_info
                        p.sell_price="$" + offer.fixedPrice() + " "
                        p.sell_location="@ " + offer.location + " ?"
                    div.small-4.columns.sell_pic
                        p=offer.user_id
                a.delete(href="/delete/" + offer._id)="Delete Offer"

回答by John Waweru

The HTTP verb your using is not correct use app.delete("/delete/:id", routes.delete_offer);

您使用的 HTTP 动词使用不当 app.delete("/delete/:id", routes.delete_offer);

I think that should work. Cause I don't think there is no delmethod in the HTTP verb for express.js framework it mostly GET, POST, PUT, DELETEplus a few others.

我认为这应该有效。因为我认为express.js 框架的 HTTP 动词中没有del方法,它主要是GET、POST、PUT、DELETE以及其他一些方法。

回答by Duy Nguyen

If you using mongoose. You can fix file routes/index.js.

如果你使用猫鼬。您可以修复文件 routes/index.js。

//Delete
exports.delete_offer = function (req,res){
  Offer.findOneAndRemove({_id : new mongoose.mongo.ObjectID(req.params.id)}, function (err,offer){
    res.redirect('/newsfeed');
  });
};

回答by ie2020

Notice if you use the Mongoose findByIdAndRemovefunction to retrieve and delete the object from the Model.

请注意,如果您使用 MongoosefindByIdAndRemove函数从Model.

exports.delete_offer = function(req, res) {

    Offer.findByIdAndRemove(req.params.id, function(err) {
        if (err)
            res.send(err);
        else
            res.json({ message: 'Offer Deleted!'});
    });
}

回答by Neil Lunn

So you have a route set up for a DELETEverb in a RESTful sense. You don't seem to be calling it that way or using it in a RESTful way.

因此,您在 RESTful 意义上为DELETE动词设置了一个路由。您似乎没有这样称呼它或以 RESTful 方式使用它。

You application should reallybe handling this as a RESTrequest, and issue status and content back as the response appropriate to what happened. Right now you are re-directing to another URL. That is not the correct approach. But If you just do not understand REST, then do it that way, but change your routeto use GETinstead.

您的应用程序应该真正将其作为REST请求处理,并将状态和内容作为对发生的事情的响应发回。现在您正在重定向到另一个 URL。这不是正确的做法。但是,如果您只是不了解 REST,那么就这样做,但更改您的路线以使用GET

For what it is worth, once you have sorted out your usage and testing, possibly using curl or similar as was shown. Then perhaps consider using .findByIdAndRemove()instead.

对于它的价值,一旦你整理了你的使用和测试,可能使用 curl 或类似的,如图所示。那么也许可以考虑使用.findByIdAndRemove()代替。

Offer.findByIdAndRemove(req.params.id, function (err,offer){
    if(err) { throw err; }
    // ...
}

And then actually checking the response is what you expect before just forwarding or sending a valid or error response. Which is what you should be doing.

然后在转发或发送有效或错误响应之前实际检查响应是您所期望的。这是你应该做的。