【问题标题】:Jade + mongodb + express.js, delete form not workingJade + mongodb + express.js,删除表单不起作用
【发布时间】:2016-05-04 07:41:21
【问题描述】:

我的删除代码不起作用,我认为甚至没有触发,因为我没有看到我的 console.log,我有一个与表单一起使用的添加按钮,它们看起来很相似,这就是我不明白的原因.

app.js:

var db = monk('localhost:27017/mongodb');

玉:

extends admin_menu

block content
    h1.
        Cocktail list
    ul
        each cocktail, i in cocktaillist
            li
                p= cocktail.name
            form#form_delete_project(name="/admin/delete_cocktail", method="post", action="/admin/delete_cocktail")
            input#input_name(type="hidden", placeholder="", name="_id", value="#{cocktail._id}")
            button#submit_project(type="submit") delete

index.js:

router.post('/admin/delete_cocktail', function(req, res) {
    console.log(id)
    // Set our internal DB variable
    var db = req.db;

    // Get our form values. These rely on the "name" attributes
    var id = req.body._id;

    // Set our collection
    var collection = db.get('cocktailcollection');

    // Submit to the DB
    collection.remove({
        "_id":id

    }, function (err, doc) {
        if (err) {
            // If it failed, return error
            res.send("There was a problem removing the information to the database.");
        }
        else {
            // And forward to success page
            res.redirect("/admin/cocktail_list");
        }
    });
});

【问题讨论】:

  • 您使用的是body-parser 中间件吗?在您启动节点服务器的终端中,您能看到 POST 请求吗?
  • 是的,我正在使用 body-parser,不,我在节点服务器上看不到任何东西

标签: mongodb express pug


【解决方案1】:

Jade 建立在缩进之上。由于您没有缩进表单中的项目,因此它不在您的表单中。在 html 中,您的代码如下所示:

<form>
</form>
<input name="_id">
<button>

由于您使用_id 输入的内容超出了表单,因此不会发布。这就是为什么您的控制台日志没有显示任何内容的原因。没有req.body._id.而且,当然,您的提交按钮也在表单之外。所以它什么都不做。

所以,你应该做的第一件事就是缩进代码。

【讨论】:

  • 谢谢,忘记缩进系统了,还是新手!所以现在我让事件触发,但它没有捕捉到发送的 id
  • 我自己更正了,我得到了 var id = req.body._id 的 ID
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-10-06
  • 2018-06-06
  • 2012-09-22
  • 2018-12-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多