Digvijay
Digvijay

Reputation: 3271

Function to return a response in NodeJS controller

I have a controller where I am trying to return a response to the user but it's unable to send response and value is showing in console.

Below is my code:

const hubHome = (req,res) => {

  const hubId = req.params.hubId;

  fetchData(hubId);
}

const fetchData = (id) => {
  console.log(`Hub id is ${id}`);
  return res.status(200).send(`Hub id is ${id}`);
}

module.exports = hubHome; 

What am I missing?

Upvotes: 0

Views: 460

Answers (1)

Jordan Wright
Jordan Wright

Reputation: 111

Currently, the res object is not within the scope of fetchData. To fix this, add another parameter to fetchData.

const hubHome = (req,res) => {

  const hubId = req.params.hubId;

  fetchData(hubId, res);
}

const fetchData = (id, res) => {
  console.log(`Hub id is ${id}`);
  return res.status(200).send(`Hub id is ${id}`);
}

module.exports = hubHome;

Upvotes: 1

Related Questions