因此,当我使用" gameName"点击按钮时,我希望能够删除MongoDB中的特定项目。值。当我这样做时,我不断收到错误404'无法删除'。我在这做错了什么?谢谢。
以下是请求:
function deleteGame() {
$('#js-games').on('click', '.delete', function(event) {
event.preventDefault();
console.log('delete button works')
var id = $(this).parent().data('id');
console.log(id);
$.ajax({
type: 'DELETE',
dataType: 'json',
contentType: 'application/json; charset=utf-8',
url: '/favorites' + '/' + id,
success: getYourGames(),
})
var gone = 'Your time was deleted!';
$('.js-type-game').html(gone);
setTimeout(function() {
$('.js-type-game').fadeOut(gone).html('').fadeIn();
}, 2000);
})
}
这是router.delete:
router.delete('/gameName', (req, res) => {
favorite.delete(req.params.gameName);
console.log(`Deleting game \`${req.params.gameName}\``);
res.status(204).end();
});
答案 0 :(得分:0)
您不小心定义了明确的/gameName
路线。要定义参数,您需要使用冒号,即:
router.delete('/:gameName', (req, res) => { // with colon (:)
favorite.delete(req.params.gameName); // req.params.gameName is now correct
console.log(`Deleting game \`${req.params.gameName}\``);
res.status(204).end();
});