0

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');
}
2
  • 1
    You just want router.post('/my_competitive_landscape', ensureAuthenticated, myCompFunc); Commented Dec 1, 2020 at 22:41
  • you are executing your function with parameters req, res that do not exist. what you want is to pass the function as a parameter, and then the caller of the function will pass req and res Commented Dec 1, 2020 at 22:42

1 Answer 1

0

Just remove the call of your function when you declare the route and declare the function before the route. Like this:

async function myCompFunc(req, res){
  let startDate = req.body.startDate
  console.log('METHOD2 startDate=', startDate)
  res.status(200).send('DONE');
}

router.post('/my_competitive_landscape', ensureAuthenticated, myCompFunc);
   
Sign up to request clarification or add additional context in comments.

2 Comments

thanks , is there any way I can specify more vars ? Something 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'); } still causes the req not defined error
figured out a way around this, by calling my function like: ` let tempResp = myCompFunc({body:{'startDate':'xqc'}}, null)` works

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.