I have a route which will work fine if I implement it with method 1:
//METHOD 1
router.post('/my_competitive_landscape', ensureAuthenticated, async function (req, res) {
let startDate = req.body.startDate
console.log('METHOD1 startDate=', startDate)
res.status(200).send('DONE');
})
Where when I make a post request to the route, it runs the function. I am trying to this function so it can be called elsewhere by changing my route to work like method2:
//METHOD 2
router.post('/my_competitive_landscape', ensureAuthenticated, myCompFunc(req, res));
async function myCompFunc(req, res){
let startDate = req.body.startDate
console.log('METHOD2 startDate=', startDate)
res.status(200).send('DONE');
}
So that when I make a post request to the route, it sends req and res to the myCompFunc function. The problem is that when I run Method2, my code wont even run because it throws me this error:
router.post('/my_competitive_landscape', ensureAuthenticated, myCompFunc(req, res));
^
ReferenceError: req is not defined
Saying req is not defined. I'm able to send req to a function in my Method1 just fine, why can I not send req using method2?
EDIT I can make it work by changing it to use myCompFunc
, but is thee a way I can specify multiple vars including but not limited to req and res? Like:
//method3
router.post('/my_competitive_landscape', ensureAuthenticated, myCompFunc(req.body.startDate, "otherVar", res));
async function myCompFunc(startDate, var2, res){
console.log('METHOD3 startDate=', startDate, ', var2=', var2)
res.status(200).send('DONE');
}