我正在創建一個RESTAPI,並且在使用POSTMAN編寫代碼之後,它在POSTMAN中返回了一條錯誤消息,並且我無法解決此問題或在Web上找到解決方案。錯誤NODE.JS,在路由
CODE:
router.route('/messages/:message_id').get(function(req, res) {
Message.findById(req.params.message_id, function(err, message) {
if (err)
res.send(err);
res.json(message);
});
});
錯誤,郵遞員,瀏覽器:
{
"stack":"Error
at MongooseError.CastError (/home/carlos/www/restapi/node_modules/mongoose/lib/error/cast.js:18:16)
at ObjectId.cast (/home/carlos/www/restapi/node_modules/mongoose/lib/schema/objectid.js:134:13)
at ObjectId.castForQuery (/home/carlos/www/restapi/node_modules/mongoose/lib/schema/objectid.js:187:17)
at module.exports (/home/carlos/www/restapi/node_modules/mongoose/lib/cast.js:205:32)
at Query.cast (/home/carlos/www/restapi/node_modules/mongoose/lib/query.js:2492:10)
at Query.findOne (/home/carlos/www/restapi/node_modules/mongoose/lib/query.js:1237:10)
at Function.findOne (/home/carlos/www/restapi/node_modules/mongoose/lib/model.js:1160:13)
at Function.findById (/home/carlos/www/restapi/node_modules/mongoose/lib/model.js:1097:15)
at /home/carlos/www/restapi/routes.js:40:17
at Layer.handle [as handle_request] (/home/carlos/www/restapi/node_modules/express/lib/router/layer.js:95:5)
at next (/home/carlos/www/restapi/node_modules/express/lib/router/route.js:131:13)
at Route.dispatch (/home/carlos/www/restapi/node_modules/express/lib/router/route.js:112:3)
at Layer.handle [as handle_request] (/home/carlos/www/restapi/node_modules/express/lib/router/layer.js:95:5)
at /home/carlos/www/restapi/node_modules/express/lib/router/index.js:277:22
at param (/home/carlos/www/restapi/node_modules/express/lib/router/index.js:349:14)
at param (/home/carlos/www/restapi/node_modules/express/lib/router/index.js:365:14)",
"message":"Cast to ObjectId failed for value \"<id>\" at path \"_id\"",
"name":"CastError",
"kind":"ObjectId",
"value":"<id>",
"path":"_id"
}
你送什麼網址? – alexmac
localhost:8080/messages/ –
Techtique
你使用真實ID還是字符串''? –
alexmac