My goal is to pass a mongoose id to an express middleware, but seems like It is not possible from my perspective, I tried to pass in req.params.id, but it returns an error
我的目标是将一个mongoose id传递给一个快速中间件,但似乎从我的角度来看这是不可能的,我试图传入req.params.id,但它返回一个错误
var middleware = require('./middleware');
app.get('/hello/:id', middleware.testing(req.params.id), function(req, res) {
// do something
})
Middleware
exports.testing = function(id) {
// do something
}
1 个解决方案
#1
1
You are using the middleware in a wrong way. A middleware already has access to req.params
. What you do is actually calling the middleware instead of passing it as a callback.
您正在以错误的方式使用中间件。中间件已经可以访问req.params。你所做的实际上是调用中间件而不是将其作为回调传递。
Use this code instead:
请改用此代码:
var middleware = require('./middleware');
app.get('/hello/:id', middleware.testing, function(req, res) {
// do something
});
Middleware
exports.testing = function(req, res) {
// do something with req.params.id
}
#1
1
You are using the middleware in a wrong way. A middleware already has access to req.params
. What you do is actually calling the middleware instead of passing it as a callback.
您正在以错误的方式使用中间件。中间件已经可以访问req.params。你所做的实际上是调用中间件而不是将其作为回调传递。
Use this code instead:
请改用此代码:
var middleware = require('./middleware');
app.get('/hello/:id', middleware.testing, function(req, res) {
// do something
});
Middleware
exports.testing = function(req, res) {
// do something with req.params.id
}