Home > Back-end >  "Cannot read property 'send' of undefined" when trying to return http status cod
"Cannot read property 'send' of undefined" when trying to return http status cod

Time:10-21

This is how my route file look like :

const express = require('express');
const asyncify = require('express-asyncify');

const router = asyncify(express.Router());

const rssController = require('../controllers/mycontroller');

router.route('/:url')
  .get(myController.handle);

module.exports = router;

This works fine and I get my json result. But I like to add some error handling and return proper http status codes.

I change my route.js file this way :

const express = require('express');
const asyncify = require('express-asyncify');

const router = asyncify(express.Router());

const rssController = require('../controllers/mycontroller');

/* router.route('/:url')
  .get(rssController.parse);            */

router.get('/:url', async (req, res) => {
  console.log(req.params);
  try {
    const result = await myController.handle(req.params);
    res.status(200).send(result);
  } catch (err) {
    res.status(500).json(err);
  }
});

module.exports = router;

Then I get an error message in my controller that says : "Cannot read property 'send' of undefined"

this is how my controller look like :

const testService = require('../services/testservice');

async function handler(url) {
  const result = await testService.HandleFile(url);
  return result;
}

const handle = async function (req, res) {
  try {
    const result = await handler(req.url);
    if (result === undefined) throw new Error('Invalid result!');
    return res.send(result);
  } catch (error) {
    console.log(`An error had happened ${error.message})`);
    return (error);
  }
};

module.exports = {
  handle,
};

Error happens in this line:

return res.send(result);

CodePudding user response:

In your ontroller you defend handle function with (req, res) but in router file you pass req.params and res argument has undefined

I suggest you to remove return res.send(result); in controller and handled result and error in router file

Sample code after remove return res.send(result);:

const rssController = require('../controllers/mycontroller');

router.get('/:url', async (req, res) => {
  console.log(req.params);
  try {
    const result = await myController.handle(req);
    res.status(200).send(result);
  } catch (err) {
    res.status(500).json(err);
  }
});

module.exports = router;
const testService = require('../services/testservice');

async function handler(url) {
  const result = await testService.HandleFile(url);
  return result;
}

const handle = async function (req) {
  try {
    const result = await handler(req.url);
    if (result === undefined) throw new Error('Invalid result!');
    return result;
  } catch (error) {
    console.log(`An error had happened ${error.message})`);
    throw error;
  }
};

module.exports = {
  handle,
};

CodePudding user response:

I have added these piece of codes in my index.route.js file for handling 404 and exceptions(500 error code)

For handling 404 pages:

router.use((req, res, next) => {
  const error = new Error('Error 404! OOOPS, page cannot be found!');
  next();
  return res.status(404).send(error.message);
});

And for other errors

router.use((error, req, res, next) => {
  next();
  return res.status(500).send(`500 Internal Server Error! An error occurred procession your request! - ${error.message}`);
});
  • Related