我正在尝试使用我的快速后端将PUT和DELETE请求发送到sqlite数据库。放置请求工作正常,但删除请求总是失败。
我已经检查了网络选项卡中的标头,它似乎是两者的正确选择(应用程序/json) 使用邮递员,我可以轻松删除条目,但使用我的前端,正文似乎设置不正确。
const countryToPush = {title: countryName}
try{
await CountryService.post(countryToPush)
console.log(countryName + ' added!')
} catch(err)
{
console.log(err)
}
},
removeFromDb: async function(countryName){
const countryToDelete = {title: countryName}
try{
await CountryService.delete(countryToDelete)
console.log(countryName + ' deleted!')
} catch(err)
{
console.log(err)
}
}
这是我的 vue 文件中,我从点击函数中获取"国家名称"。
try {
const country = await Country.create(req.body)
res.send(country)
} catch (err) {
res.status(500).send({
error: 'an error has occurred trying to create the country'
})
}
},
async delete (req, res) {
try {
const toDelete = await Country.findOne({
where: {
title: req.body.title
}
})
await toDelete.destroy()
res.send(req.body.title + ' was deleted')
} catch (err) {
res.status(500).send({
error: 'an error has occurred trying to delete the country'
})
}
}
而这是我的 sqlite 调用中的示例
不幸的是,来自我的 vue 前端的 DELETE 请求总是失败,并给我定义的错误 500an error has occurred trying to delete the country.
有什么想法,我还能尝试让它工作吗?
Model.delete()
不是Sequelize函数,你想使用Model.destroy()
。
await CountryService.destroy(countryToDelete)
现在您正在吞下实际的错误 - 将console.log(err)
添加到您的捕获中,以查看它可能在说CountryService.destroy is undefined
.