0

I have some code that usually works, but twice now has produced the following error:

TypeError: Cannot read property 'send' of undefined

The code is:

app.user.get('/status.json', mGatewayTimeout, function (req, res) {
  var user = req.user
  var qs = cu.querystring.parseUrl(req.url)
  if (user.apps && user.apps.beeminder && user.apps.beeminder.access_token) {
    if (bsCache[user.username] && !qs.force) {
      res.send(bsCache[user.username])
    } else {
      var bee = new Bee({access_token: req.user.apps.beeminder.access_token})
      bee.getUserSkinny(function (err, bm_user) {
        if (err) {
          bsCache[user.username] = null
          return res.status(500).send('error: ' + err.toString())

So that last line produces the TypeError when it tries to call .send on res.status(500).

I've left in a whole bunch of stuff that is certainly irrelevant, because if I tried to take out everything that I thought was irrelevant, we'd be left with nothing.

Bee is an API client, but one that I wrote, so I'm not sure it isn't doing something weird, except that I can't see how it could be affecting the response object.

MalcolmOcean
  • 2,807
  • 2
  • 29
  • 38

2 Answers2

0

Try res.status(500).send(`error ${err.message}`), new Error's are objects made from a constructor and one of the properties is message.

Colin Daniel
  • 180
  • 10
  • That's... good advice! But not relevant to the problem I'm having, which is that the send function can't be found on the other object. – MalcolmOcean Feb 22 '19 at 21:14
  • @MalcolmOcean Sorry, my last guess is that you need to set the [content type](https://stackoverflow.com/questions/23714383/what-are-all-the-possible-values-for-http-content-type-header) beforehand using .set(). – Colin Daniel Feb 23 '19 at 01:32
0

Oh man, I'm a fool. I made my own problem here. This was on an endpoint that accesses an external API, and since I'm on heroku I need all requests to take under 30s, so I wrote some timeout code that would send an error then turn various functions like res.send into no-op functions. Problem, of course, was that I didn't proceed to return the res object in those no-ops.

‍♀️

MalcolmOcean
  • 2,807
  • 2
  • 29
  • 38