2016-11-23 59 views
-1

這是我的代碼片段,我在哪裏使用Express路由器和Mongoose模型。無法在expressjs中訪問參數router.delete

我無法訪問id parm。

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

 
     } 
 
    }); 
 

 

 
});

這是我收到

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)

回答

3

的對象不是parms訪問路由參數時,錯誤的params.由於parms總是將是undefined你」因爲您試圖訪問您無法做到的undefined屬性,因此您將會遇到此問題。

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

謝謝..它工作正常 –

相關問題