[英]params looking for id being fired on another route when it shouldn't
我是貓鼬的新手,因此對不正確使用術語表示歉意。
我有一個路由文件,如下所示
const express = require('express');
const router = express.Router();
const passport = require('passport');
const controller = require('./clubController');
const authGuard = passport.authenticate('jwt', { session: false });
const verifyUser = require('./clubController').verifyUser;
const isSiteAdmin = require('./clubController').isSiteAdmin;
router.param('id', controller.params);
router.route('/')
.post(authGuard, controller.newClub)
.get(controller.allPublicClubs);
router.route('/:id')
.put(authGuard, verifyUser(), controller.editClub)
.get(controller.getClub);
router.route('/private')
.get(controller.allPrivateClubs);
module.exports = router;
exports.params = function(req, res, next, id) {
Club.findById(id)
.populate('creator teams', '-password -email -role')
.exec()
.then(function(club) {
if (!club) {
return res.status(404).send({ msg: 'No Club exists with that ID' });
} else {
req.club = club;
next();
}
}, function(err) {
// error handling
next(err);
});
};
當我向/private
發出get請求時,會觸發controller.params
。 據我了解,只有當被調用的路由使用id
參數時,才應啟動我已設置的params中間件。
controller.params
id
參數的值設置為private
,即路由。
我收到的錯誤在以下CastError: Cast to ObjectId failed for value "private" at path "_id" for model "club"
中進行了詳細CastError: Cast to ObjectId failed for value "private" at path "_id" for model "club"
昨天運行良好,不知道發生了什么變化,現在不起作用。
我通過移動解決了這個問題
router.route('/:id')
.put(authGuard, verifyUser(), controller.editClub)
.get(controller.getClub);
到所有路線的底部。 很奇怪,因為我之前有相同的訂單,而且效果很好
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.