Home > front end >  Function to return a response in nodejs controller
Function to return a response in nodejs controller

Time:11-23

I have a controller where I am trying to return a response to the user but its 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; 

Someone let me know what I am missing.Any help would be apprecicted.

CodePudding user response:

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;
  • Related