Reacord已从数据库中删除,但出现错误:-使用axios,node,mongo和Koa的请求失败,状态代码为404

问题描述 投票:0回答:1

数据已成功删除,但代码GEThttp://localhost:3000/api/deletetagbyid/5e4d896cea390804241612cd404(未找到)出现错误。

任何人都可以帮助我,实际上我是React节点中的新手:-感谢您

// Delete Discount Tag Using Axios :-
const handleDelete = async (e, id) => {

    if (confirm("Are you sure. You Want to delete this tag?")) {

        await axios.get("/api/deletetagbyid/" + id).then((result) => {
            console.log(result);
        }).catch(() => {
            console.log('Error Occured');
        });
    }
}

// This is the backend code to delete data with koa js :-
exports.deletetagbyid = (ctx) => {

    let id = ctx.params.id;
    Note.findOne({
        "_id": ObjectID(id)
    }, function (err, docs) {

        if (!docs) {
            ctx.body = 404;
        } else {
            Note.deleteOne({
                _id: id
            }, function (err, obj) {
                if (err) throw err;
                ctx.body = "Deleted SuccessFully";
                console.log(" document(s) deleted");
            });
        }
    })
};

// This is the route for delete data :-
router.get('/api/deletetagbyid/:id', discounttag.deletetagbyid);
node.js reactjs mongodb koa
1个回答
0
投票

抱歉,请不要使用get方法从数据库中删除记录,请使用DELETE方法

对于您的问题,我认为您已经两次调用了删除api

© www.soinside.com 2019 - 2024. All rights reserved.