通过 Fetch API,我发送以下请求:
const target = e.currentTarget;
fetch(target.href, {
method: 'delete',
})
.then(res => console.log(22))
.catch(err => console.log(err));
此外,这是处理此请求的中间件:
exports.deleteImageById = async (req, res) => {
const image_id = req.params.image_id;
const imagePromise = Image.findByIdAndRemove(image_id);
const commentPromise = Comment.remove( {image_id} );
await Promise.all([imagePromise, commentPromise])
.catch(err => console.log(err));
req.flash('Image Deleted!');
// return something well!
res.status(200);
};
正在删除文档,但 fetch 语句中的 then
block 不起作用。它不会向控制台输出任何内容。
我在这里做错了什么吗?
最佳答案
未收到响应,因此 then
将不会被调用。在服务器中更改
res.status(200);
包含 end()
自 status不发送响应
res.status(200).end();
res.sendStatus(200);
关于javascript - 获取删除请求无法正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50234396/