![](/img/trans.png)
[英]how can I differentiate mongoDB query by value of req.params.id in node.js?
[英]Node.js req.params.locationid not grabbing ID
我有以下帶有特殊ID的網址:
http://localhost:3000/location/5733e37adcba0f6d5aa88cf5/review/new
這是一個頁面,其中包含一個表格,該表格將被填寫並提交給api並保存在我的數據庫中。 我遇到的一個奇怪的問題是,在我的控制器中
req.params.locationid
由於某種原因返回undefined
。
這是我的控制器代碼:
module.exports.doAddReview = function(req, res) {
var requestOptions, path, locationid, postdata;
locationid = req.params.locationid;
console.log("\n>>>> " + req.params.locationid + " <<<<\n");
path = "/api/locations/" + locationid + '/reviews';
postdata = {
author: req.body.name,
rating: parseInt(req.body.rating, 10),
reviewText: req.body.review
};
requestOptions = {
url : apiOptions.server + path,
method : "POST",
json : postdata
};
request(requestOptions, function(err, response, body) {
if (response.statusCode === 201) {
res.redirect('/location/' + locationid);
} else {
_showError(req, res, response.statusCode);
}
});
};
任何想法為什么req.params.locationid
在我的其他控制器中運行良好,但在這一個中它不是出於某種原因? 我的路由器可能錯了嗎?
順便說一句,表格如下:
action="", method="post", role="form"
和路由器:
/* Locations pages */
router.get('/', ctrlLocations.homelist);
router.get('/location/:locationid', ctrlLocations.locationInfo);
router.get('/location/:locationid/review/new', ctrlLocations.addReview);
router.post('/location/:locaitonid/review/new', ctrlLocations.doAddReview);
因為您在路由器文件中輸入錯誤。
router.post('/location/:locationid/review/new', ctrlLocations.doAddReview);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.