1
votes

Here is my Snippet of Code, Where I used Express router and Mongoose Model .

I'm Unable to Access id parm .

router.delete('/task/:id', function (req, res) {
    Task.remove({ did: req.parms.id }, (err) => {
        if (err) {
            res.send(err);

        }
    });


});

This is the Error I received

TypeError: Cannot read property 'id' of undefined at F:\NS\routes\tasks.js:11:33 at Layer.handle [as handle_request] (F:\NS\node_modules\express\lib\router\layer.js:95:5) at next (F:\NS\node_modules\express\lib\router\route.js:131:13) at Route.dispatch (F:\NS\node_modules\express\lib\router\route.js:112:3) at Layer.handle [as handle_request] (F:\NS\node_modules\express\lib\router\layer.js:95:5) at F:\NS\node_modules\express\lib\router\index.js:277:22 at param (F:\NS\node_modules\express\lib\router\index.js:349:14) at param (F:\NS\node_modules\express\lib\router\index.js:365:14) at Function.process_params (F:\NS\node_modules\express\lib\router\index.js:410:3) at next (F:\NS\node_modules\express\lib\router\index.js:271:10) at Function.handle (F:\NS\node_modules\express\lib\router\index.js:176:3) at router (F:\NS\node_modules\express\lib\router\index.js:46:12) at Layer.handle [as handle_request] (F:\NS\node_modules\express\lib\router\layer.js:95:5) at trim_prefix (F:\NS\node_modules\express\lib\router\index.js:312:13) at F:\NS\node_modules\express\lib\router\index.js:280:7 at Function.process_params (F:\NS\node_modules\express\lib\router\index.js:330:12) at next (F:\NS\node_modules\express\lib\router\index.js:271:10) at F:\NS\node_modules\express-validator\lib\express_validator.js:291:5 at Layer.handle [as handle_request] (F:\NS\node_modules\express\lib\router\layer.js:95:5) at trim_prefix (F:\NS\node_modules\express\lib\router\index.js:312:13) at F:\NS\node_modules\express\lib\router\index.js:280:7 at Function.process_params (F:\NS\node_modules\express\lib\router\index.js:330:12)

2

2 Answers

4
votes

The object isn't parms when accessing a route parameter, its params. Since parms is always going to be undefined you're going to have this issue because you're trying to access a property of undefined which you can't do.

router.delete('/task/:id', (req, res) => {
    Task.remove({ did: req.params.id }, (err) => {
        if (err) 
            return res.send(err);
    });
});
0
votes

you will probably see this when you run the code but, Model.remove() is deprecated. Instead, I recommend Model.deleteOne() in the case in a future update it is no longer supported.