Sunkhern
Sunkhern

Reputation: 278

Jade + mongodb + express.js, delete form not working

My delete code is not working and I think not even firing as I don't see my console.log, I have an add button that works with a form and they look alike, this is why I don't get it.

app.js:

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

Jade:

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");
        }
    });
});

Upvotes: 0

Views: 596

Answers (1)

tomtom
tomtom

Reputation: 642

Jade is built on indentation. Since you are not indenting the items in your form it is not in you form. In html your code would look like this:

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

Since your input with _id is outside the form it is not being posted. That is why your console log is showing nothing. There is no req.body._id.And, of course, your submit-button is also outside the form. So it does nothing.

So, the first thing you should do is indent the code.

Upvotes: 1

Related Questions