这个删除过程node.js缺少什么?

时间:2018-03-25 02:45:11

标签: mysql node.js

编辑工作正常,但它似乎与删除有根本的不同,因为编辑实际上转到了不同的页面。这是一个项目的一部分,我们有点拼凑给我们的代码并在这里和那里添加几行,但我不知道删除中缺少什么。

请求获取项ID,查询看起来像是对我的正确删除功能,其余只是打印结果?删除按钮有效,但这就是我所能看到的。

enter image description here enter image description here

删除 -

app.delete('/delete/(:id)', function(req, res, next) {
    req.getConnection(function(error, conn) {
        conn.query('DELETE from store where id = ' + req.params.id, function(err, result) {

            if (rows.length <= 0) {
                req.flash('error', err)
                // redirect to users list page
                res.redirect('/store')
            } else {
                req.flash('success',

                    )
                // redirect to users list page
                res.redirect('/store')
            }
        })
    })
})

编辑 -

app.get('/edit/(:id)', function(req, res, next) {

    req.getConnection(function(error, conn) {
        conn.query( 
            'SELECT * From store Where id = ' + req.params.id, function(err, rows, field) {
            // if item not found
            if (rows.length <= 0) {
                req.flash('error', 'Item not found with id = ' +
                    id )
                res.redirect('store/edit')
            } else { // if item found
                // render to views/store/edit.ejs template file
                res.render( 'store/edit' , {
                    title: 'Edit Item',

                    id: req.params.id,
                    qty:rows[0].qty ,
                    price: rows[0].price,
                    sname: rows[0].sname
                })
            }
        })
    })
})


app.put('/edit/(:id)', function(req, res, next) {
    req.assert('sname', 'Name is required').notEmpty()
    //Validate name
    req.assert('qty', 'Quantity is required').notEmpty()
    //Validate qty
    req.assert('price', 'Price is required').notEmpty()
    //Validate price
    var errors = req.validationErrors()
    if (!errors) { //No errors were found. Passed Validation!

    var item = {
        sname: req.sanitize('sname').escape().trim(),
        qty: req.sanitize('qty').escape().trim(),
        price: req.sanitize('price').escape().trim()
    }

        req.getConnection(function(error, conn) {
            conn.query('Update store set ? where id =' + req.params.id, item, function(err, result) {
                if (err) {
                    req.flash('error', err)
                    // render to views/store/edit.ejs
                    res.render('store/edit', {
                        id: req.params.id,
                        sname: item.sname,
                        qty: item.qty,
                        price: item.price
                    })

                } else {
                    req.flash('success', 'Data updated successfully!')
                    // render to views/store/edit.ejs
                    res.render('store/edit', {
                        title: 'Edit Item',
                        id: req.params.id,
                        sname: item.sname,
                        qty: item.qty,
                        price: item.price
                    })
                }
            })
        })

    }
    else {
        var error_msg = ''
        errors.forEach(function(error) {
            error_msg += error.msg + '<br>'
        })
        req.flash('error', error_msg)

        res.render( '/store/edit', {
            title: 'Edit Item,',
            id: req.params.id,
            sname: req.body.sname,
            qty: req.body.qty,
            price: req.body.price ,
        })
    }
})

表单内容 -

<form method="post" action="/store/delete/<%= item.id %>"
        style="float:right">
        </form>
        <input type="submit" name="delete" value='Delete'
        onClick="return confirm('Are you sure you want to delete?')" />
        <input type="hidden" name="_method" value="DELETE" 

2 个答案:

答案 0 :(得分:0)

<form action="POST">发送POST个请求,而不是DELETE HTTP请求。

HTML表单只能action="POST"action="GET"。你需要:

  • 在这种情况下,使用POST进行删除。
  • 使用javascript执行DELETE请求。 (ajax-ish request)

答案 1 :(得分:0)

我猜您delete的表单在输入值之前会被关闭。

更改html如下:

<form method="post" action="/store/delete/<%= item.id %>"
    style="float:right">
    <input type="submit" name="delete" value='Delete'
    onClick="return confirm('Are you sure you want to delete?')" />
    <input type="hidden" name="_method" value="DELETE"/>
 </form>