假設我有以下 REST 端點
/api/companies
/api/companies/:id
/api/companies/name/:slug
第一條路線/api/companiesPOST 將發布一個新公司,而 GET 將獲取所有公司的串列。
第二條路線/api/companies/:idGET 將通過其 獲取該特定公司id,PUT 將更新它,DELETE 將洗掉它。
第三條路線/api/companies/name/:slug將獲取以 開頭的公司串列name。
我的router.js檔案看起來像這樣
// /api/companies
router
.route('/')
.post(controllers.createCompany)
.get(controllers.getByPriority)
// /api/companies/:id
router
.route('/:id')
.put(controllers.updateOne)
.delete(controllers.removeOne)
.get(controllers.getOne)
// /api/companies/name/:slug
router
.route('/name/:slug?')
.get(controllers.getByName)
問題是如果第三個路由中沒有slug引數,它將與第二個路由匹配,這將回傳轉換錯誤。
例如:GET /api/companies/name/回傳如下錯誤
CastError: Cast to ObjectId failed for value "name" (type string) at path "_id" for model "company"
at model.Query.exec (D:\Projects\StackInfo\stack-info\backend\node_modules\mongoose\lib\query.js:4545:21)
at D:\Projects\StackInfo\stack-info\backend\dist\utils\crud.js:91:15
at Layer.handle [as handle_request] (D:\Projects\StackInfo\stack-info\backend\node_modules\express\lib\router\layer.js:95:5)
at next (D:\Projects\StackInfo\stack-info\backend\node_modules\express\lib\router\route.js:137:13)
at next (D:\Projects\StackInfo\stack-info\backend\node_modules\express\lib\router\route.js:131:14)
at next (D:\Projects\StackInfo\stack-info\backend\node_modules\express\lib\router\route.js:131:14)
at next (D:\Projects\StackInfo\stack-info\backend\node_modules\express\lib\router\route.js:131:14)
at next (D:\Projects\StackInfo\stack-info\backend\node_modules\express\lib\router\route.js:131:14)
at next (D:\Projects\StackInfo\stack-info\backend\node_modules\express\lib\router\route.js:131:14)
at next (D:\Projects\StackInfo\stack-info\backend\node_modules\express\lib\router\route.js:131:14) {
messageFormat: undefined,
stringValue: '"name"',
kind: 'ObjectId',
value: 'name',
path: '_id',
reason: TypeError: Argument passed in must be a Buffer or string of 12 bytes or a string of 24 hex characters
at new BSONTypeError (D:\Projects\StackInfo\stack-info\backend\node_modules\bson\lib\error.js:39:42)
at new ObjectId (D:\Projects\StackInfo\stack-info\backend\node_modules\bson\lib\objectid.js:62:23)
at castObjectId (D:\Projects\StackInfo\stack-info\backend\node_modules\mongoose\lib\cast\objectid.js:25:12)
at ObjectId.cast (D:\Projects\StackInfo\stack-info\backend\node_modules\mongoose\lib\schema\objectid.js:245:12)
at ObjectId.SchemaType.applySetters (D:\Projects\StackInfo\stack-info\backend\node_modules\mongoose\lib\schematype.js:1135:12)
at ObjectId.SchemaType._castForQuery (D:\Projects\StackInfo\stack-info\backend\node_modules\mongoose\lib\schematype.js:1567:15)
at ObjectId.SchemaType.castForQuery (D:\Projects\StackInfo\stack-info\backend\node_modules\mongoose\lib\schematype.js:1557:15)
at ObjectId.SchemaType.castForQueryWrapper (D:\Projects\StackInfo\stack-info\backend\node_modules\mongoose\lib\schematype.js:1534:20)
at cast (D:\Projects\StackInfo\stack-info\backend\node_modules\mongoose\lib\cast.js:336:32)
at model.Query.Query.cast (D:\Projects\StackInfo\stack-info\backend\node_modules\mongoose\lib\query.js:4968:12),
valueType: 'string'
}
getOnecrud.js看起來也像這樣
export const getOne = model => async (req, res) => {
try {
const id = req.params.id
const doc = await model
.findOne({ _id: id })
.lean().exec() // Where the error exists
if (!doc) {
return res.status(404).end()
}
res.status(200).json({ data: doc })
} catch (e) {
console.error(e)
res.status(400).end()
}
}
我嘗試添加其他路由,以便它可以在沒有slug傳遞的情況下處理請求
// /api/companies
router
.route('/')
.post(controllers.createCompany)
.get(controllers.getByPriority)
// /api/companies/:id
router
.route('/:id')
.put(controllers.updateOne)
.delete(controllers.removeOne)
.get(controllers.getOne)
// in case /api/companies/name/:slug did not match
router
.route('/name')
.get(controllers.getByPriority)
// /api/companies/name/:slug
router
.route('/name/:slug?')
.get(controllers.getByName)
但它仍然與/api/companies/:id路由匹配并回傳相同的錯誤。那么,即使使用空引數,/api/companies/:id當我請求時,是否有任何解決方案可以避免路由?/api/companies/name/
uj5u.com熱心網友回復:
從上到下快速執行代碼。因此,您可以顛倒代碼中端點的順序。由于/name/:slug更具體,請在之前定義它/:id:
router
.route('/')
.post(controllers.createCompany)
.get(controllers.getByPriority)
router
.route('/name/:slug?')
.get(controllers.getByName)
router
.route('/:id')
.put(controllers.updateOne)
.delete(controllers.removeOne)
.get(controllers.getOne)
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/427158.html
標籤:javascript 节点.js 表示 快速路由器
