因此,在名为 books.js 的哈巴狗文件中有以下路由器,在该路由器中,我正在使用Sequelize ORM根据ID查找一行数据,以便删除>
/* - Deletes a book. Careful, this can’t be undone.
It can be helpful to create a new “test” book to test deleting.
create the post /books/:id/delete route*/
router.post('/:id/delete', function(req, res, next){
Book.findOne({where: {id: req.params.id}}).then(function(book){
return book.destroy();
}).then(function(){
res.redirect('/books/');
})
});
这是一个名为 update-book.pug 的哈巴狗文件中的表格,其中有一个按钮,一旦按下该按钮,它应该删除数据行并重定向到/ books
form(action="/books/" + book.id , method="post" onsubmit="return confirm('Do you really want to delete this book?');")
按下删除按钮后,我得到200(ok)状态代码,但浏览器停留在同一页面上
有人可以帮忙吗?供参考,这是我的仓库https://github.com/SpaceXar20/sql_library_manager-updated
答案 0 :(得分:0)
您已将一个表单的方法定义为“ POST”,并且您的路由器需要一个“ DELETE”方法。因此,要么更改路由器以接受“ POST”方法,要么使用“ DELETE”方法发出AJAX请求。
答案 1 :(得分:0)
var control = $('#control');
let mousedown = false;
var zone = 2;
const SNAP_TARGET = 1;
const SNAP_DISTANCE = 0.1;
const LEEWAY = 0.01;
function getSliderInfo() {
const sliderVal = parseFloat(control.val());
return [sliderVal, sliderVal - SNAP_TARGET];
}
//control.on('click',function(){console.log("changed")})
control.on('input', function () {
const [position, difference] = getSliderInfo();
let offset = -difference;
if (difference < 0 && zone === 3) {
if (difference <= -SNAP_DISTANCE) {
zone = 1;
}
}
else if (difference > 0 && zone === 1) {
if (difference >= SNAP_DISTANCE) {
zone = 3;
}
}
else {
offset = 0;
if (zone === 2) {
if (Math.abs(difference) > LEEWAY) {
zone = difference < 0 ? 1 : 3;
}
else {
control.val(1);
}
}
}
control.val(position + offset);
});
$(document).on('mouseup',function(){
if(mousedown){
change();
mousedown = false;
}
})
control.on('mousedown',() => mousedown = true);
function change() {
const difference = getSliderInfo()[1];
if (Math.abs(difference) <= LEEWAY) {
control.val(1);
zone = 2;
}
console.log('change event occured');
}
control.on('change', change);
答案 2 :(得分:0)
当可以在sequelize的destroy函数中的何处放置条件时,为什么要使用两个单独的查询?作为回应,您得到删除的记录数,并可以检查是否删除了任何记录。
答案 3 :(得分:0)
因为您正在使用return语句,所以您的代码应为:
try{
await Book.destroy({where:{id:req.params.id})
res.redirect('/')
}
catch(e){
console.log(e)
// handle error better
}
您也不需要查找然后删除此查询,查找并自动删除