从next()调用req undefined

时间:2014-10-06 17:05:11

标签: node.js express routing undefined

//在routes.js

test  = require('test');

app.get('/test', function(req, res) {
  test.first(
    req,res,
    test.second,
    'info'
  );
});

//在test.js

exports.first= function(req, res, next, inf){
    req.test= inf;
    console.log(inf); //info
    next();
};
exports.second= function(req, res, next){
    console.log(req); //undefined
    res.jsonp(req.test);//error :s
};

这是来自how to call a controller with express routes and include a defined parameter

的后续问题

1 个答案:

答案 0 :(得分:4)

假设您在next();中呼叫test.first(),则表示您未向test.second()传递任何参数。因此,对于您当前的设置,您需要在next(req, res);test.first()代替。