我试图为一家旅游公司构建一个api,但是出现了错误“TypeError:Cannot create property'next'on string‘/:id’”。这里列出的所有函数都是创建的
const express = require('express');
const port = 3000
const app = express();
const tourRouter = express.Router();
tourRouter.route('/').
get(getAllTours)
.post(createTour);
tourRouter('/:id').
get(getTour)
.patch(updateTour)
.delete(deleteTour);
app.use('/api/v1/tours', tourRouter)
app.listen(port)
错误是
req.next = next; ^
TypeError: Cannot create property 'next' on string '/:id'
at Function.handle (C:\Users\dell\OneDrive\Desktop\node\starter\node_modules\express\lib\router\index.js:160:12)
at router
您似乎忘了.请在此处使用route
:
tourRouter('/:id').
应该是
tourRouter.route('/:id').