简体   繁体   English

尝试使用Express.js和Jade从MongoDB数据库中删除条目

[英]Trying to delete entry from MongoDB database using Express.js & Jade

I created an app that adds and removes 'Case Studies'. 我创建了一个添加和删除“案例研究”的应用。 I am able to add entries easily, but when I try to delete an entry, I am redirected of the form page with the form action URL appended to the end. 我能够轻松添加条目,但是当我尝试删除条目时,将重定向到表单页面,并将表单操作URL追加到末尾。 The entry does in fact get deleted when I go back to the original form page, but ideally I'd like to be able to stay on the same page as I add/delete entries. 实际上,当我返回原始表单页面时,该条目确实会被删除,但是理想情况下,我希望能够与添加/删除条目保持在同一页面上。

form.jade form.jade

form(method="POST", action="/form/#{entry._id}?_method=DELETE")
    input(type="submit", value="Delete")

form.js form.js

/* Delete Case Study */
router.delete('/:id', function (req, res) {
  Entry.findById(req.params.id)
    .exec(function(err, entries) {
       // changed `if (err || !doc)` to `if (err || !entries)`
        if (err || !entries) {
            res.statusCode = 404;
            res.send({});
        } else {
            entries.remove(function(err) {
                if (err) {
                    res.statusCode = 403;
                    res.send(err);
                } else {
                    res.send('Deleted entry with link: ', entries.link);
                }
            });
        }
    });
});

Here is a link to my app on Heroku, as well as my GitHub repo: 这是我在Heroku上的应用程序以及GitHub存储库的链接:

https://hidden-wave-1121.herokuapp.com/form https://hidden-wave-1121.herokuapp.com/form

https://github.com/hlmurray/express-app https://github.com/hlmurray/express-app

Any help here would be great, thank you! 这里的任何帮助将是巨大的,谢谢!

You can use a javascript http request instead of a form post/submission and wire it with an onclick function on your delete button, heres a StackOverflow Question which covers http requests. 您可以使用javascript http请求而不是表单发布/提交,并在您的删除按钮上使用onclick函数对其进行连线,这是一个涵盖http请求的StackOverflow问题。

input(type="button", onclick="deleteItem({entry._id})")

function deleteItem(theID)
{
    var xmlHttp = new XMLHttpRequest();
    xmlHttp.open( "DELETE", '/form/'+ theID, false );
    xmlHttp.send( null );
    return xmlHttp.responseText;
}

Also it looks like you'll need to update your route in form.js so that it reads /forms/:id. 同样,您似乎需要在form.js中更新路由,以使其读取/ forms /:id。

That won't get rid of the HTML on your page though, so you'll need some more javascript to delete that DOM element. 但是,这不会摆脱页面上的HTML,因此您将需要更多的JavaScript来删除该DOM元素。 Usually when I work with the MEAN stack, I do all my form posts and calls using the Angular $http method, that way you don't get any page changes. 通常,当我使用MEAN堆栈时,我会使用Angular $ http方法完成所有表单发布和调用,这样您就不会进行任何页面更改。 I still use forms, but instead of the standard submit, I use ng-submit, which prevents page changes. 我仍然使用表单,但是我使用ng-submit代替了标准提交,这可以防止页面更改。

Hope that helps, let me know if I can include anything else, and I'll update the answer. 希望对您有所帮助,让我知道是否可以添加其他内容,然后我将更新答案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM