2017-01-16 75 views
0
if (token_count == 1) { 
    var user_name = rows[0].user_name; 
    next(); 
} else { 
    data = { 
     message :"Invalid Token" 
    } 
    res.send(data); 
} 

我需要通過USER_NAME從參數的next(),它被調用的函數爲波紋管,傳遞參數的NodeJS

router.post('/dashboard', function (req, res) { 
    // user_name must be fetched here 
    console.log("middleware next") 
}); 
+0

我已經通過使用res.locals解決了它 res.locals.user_name = user_name; –

回答

0

您可以將數據添加到REQ對象

if (token_count == 1) { 
      var user_name = rows[0].user_name; 
      req.user_name = user_name; 
      next(); 
     }else{ 
      data = { 
      message :"Invalid Token" 
      } 
      res.send(data); 
     } 


    router.post('/dashboard', function (req, res) { 
    // user_name must be fetched here 
    console.log(req.user_name) 
    });