提问者:小点点

TypeError:无法在字符串“/:id”上创建属性“next”


我试图为一家旅游公司构建一个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 

共1个答案

匿名用户

您似乎忘了.请在此处使用route:

tourRouter('/:id').

应该是

tourRouter.route('/:id').