GreyGhost
GreyGhost

Reputation: 33

Delete route having issue: params of undefined

I am creating a delete route and I have been getting error from this delete route. I don't usually get this problem. I consolelog the error, it complains about req.params.bookid: "TypeError: Cannot read property 'bookid' of undefined"... I tried changing to the old 'callback' syntax, the problem persist. What on earth happened here? This is my code:

router.delete("/greybook/:bookid", async (res, req) => {
   try {
        let deletedBook = await Book.findByIdAndRemove(req.params.bookid);
        req.flash("success", "Successfully deleted " + deletedBook.title);
        res.redirect("/greybook");  

    } catch(error) {
        req.flash("error", "9:Error trying to delete book, please try again...");
        res.redirect("/greybook");
    }
});

// Problem persists when using the callbacks syntax:

router.delete("/greybook/:bookid", (res, req) => {
    Book.findByIdAndRemove(req.params.bookid, (error, deletedBook) => {
        if(error) {
            console.log(error);
        }
        req.flash("success", "Successfully deleted " + deletedBook.title);
        res.redirect("/greybook");  
    });
});

This is the error message I get:

TypeError: Cannot read property 'bookid' of undefined
    at router.delete (/home/greyghost/Documents/ggtest/greyBook/vrs5/routes/greybook.js:107:36)
    at Layer.handle [as handle_request] (/home/greyghost/Documents/ggtest/greyBook/vrs5/node_modules/express/lib/router/layer.js:95:5)
    at next (/home/greyghost/Documents/ggtest/greyBook/vrs5/node_modules/express/lib/router/route.js:137:13)
    at Route.dispatch (/home/greyghost/Documents/ggtest/greyBook/vrs5/node_modules/express/lib/router/route.js:112:3)
    at Layer.handle [as handle_request] (/home/greyghost/Documents/ggtest/greyBook/vrs5/node_modules/express/lib/router/layer.js:95:5)
    at /home/greyghost/Documents/ggtest/greyBook/vrs5/node_modules/express/lib/router/index.js:281:22
    at param (/home/greyghost/Documents/ggtest/greyBook/vrs5/node_modules/express/lib/router/index.js:354:14)
    at param (/home/greyghost/Documents/ggtest/greyBook/vrs5/node_modules/express/lib/router/index.js:365:14)
    at Function.process_params (/home/greyghost/Documents/ggtest/greyBook/vrs5/node_modules/express/lib/router/index.js:410:3)
    at next (/home/greyghost/Documents/ggtest/greyBook/vrs5/node_modules/express/lib/router/index.js:275:10)
    at Function.handle (/home/greyghost/Documents/ggtest/greyBook/vrs5/node_modules/express/lib/router/index.js:174:3)
    at router (/home/greyghost/Documents/ggtest/greyBook/vrs5/node_modules/express/lib/router/index.js:47:12)
    at Layer.handle [as handle_request] (/home/greyghost/Documents/ggtest/greyBook/vrs5/node_modules/express/lib/router/layer.js:95:5)
    at trim_prefix (/home/greyghost/Documents/ggtest/greyBook/vrs5/node_modules/express/lib/router/index.js:317:13)
    at /home/greyghost/Documents/ggtest/greyBook/vrs5/node_modules/express/lib/router/index.js:284:7
    at Function.process_params (/home/greyghost/Documents/ggtest/greyBook/vrs5/node_modules/express/lib/router/index.js:335:12)

Upvotes: 1

Views: 349

Answers (1)

fedesc
fedesc

Reputation: 2610

You need to change the order of req and res in your routes to (req,res). First comes the request.

router.delete("/greybook/:bookid", async (req,res) => {
   try {
        let deletedBook = await Book.findByIdAndRemove(req.params.bookid);
        req.flash("success", "Successfully deleted " + deletedBook.title);
        res.redirect("/greybook");  

    } catch(error) {
        req.flash("error", "9:Error trying to delete book, please try again...");
        res.redirect("/greybook");
    }
});


router.delete("/greybook/:bookid", (req,res) => {
    Book.findByIdAndRemove(req.params.bookid, (error, deletedBook) => {
        if(error) {
            console.log(error);
        }
        req.flash("success", "Successfully deleted " + deletedBook.title);
        res.redirect("/greybook");  
    });
});

Upvotes: 1

Related Questions