hubotio / hubot

A customizable life embetterment robot.

Home Page:https://hubotio.github.io/hubot/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

JSON parse res is not defined Error

herz0g opened this issue · comments

Hi, I try to parse the body from here:
http://www.tableflipper.com/json

But I receive this errors during Hubot startup:

Feb 28 08:00:11 hubot hubot[8293]: [Thu Feb 28 2019 08:00:11 GMT+0100 (CET)] ERROR ReferenceError: res is not defined
Feb 28 08:00:11 hubot hubot[8293]:   at /home/bamboo/slack-hubot/scripts/tableflip.coffee:9:7, <js>:6:7
Feb 28 08:00:11 hubot hubot[8293]:   at IncomingMessage.<anonymous> (/home/bamboo/slack-hubot/node_modules/scoped-http-client/src/index.js:95:22)
Feb 28 08:00:11 hubot hubot[8293]:   at emitNone (events.js:111:20)
Feb 28 08:00:11 hubot hubot[8293]:   at IncomingMessage.emit (events.js:208:7)
Feb 28 08:00:11 hubot hubot[8293]:   at endReadableNT (_stream_readable.js:1064:12)
Feb 28 08:00:11 hubot hubot[8293]:   at _combinedTickCallback (internal/process/next_tick.js:138:11)
Feb 28 08:00:11 hubot hubot[8293]:   at process._tickCallback (internal/process/next_tick.js:180:9)

My tableflip.coffee is based on the JSON documentation: https://hubot.github.com/docs/scripting/

module.exports = (robot) ->

  robot.http("http://www.tableflipper.com/json")
    .header('Accept', 'application/json')
    .get() (err, response, body) ->
      # error checking code here

      data = JSON.parse body
      res.send "#{data.gif}"

Whats wrong with my code?

Also this one is not working (error: Feb 28 08:27:29 hubot hubot[8518]: [Thu Feb 28 2019 08:27:29 GMT+0100 (CET)] ERROR Unable to load /home/bamboo/slack-hubot/scripts/tableflip: ReferenceError: robot is not defined)

module.exports = (robot) ->

robot.respond /flip/i, (res) ->

res.http("http://www.tableflipper.com/json")
    .header('Accept', 'application/json')
    .get() (err, response, body) ->
      # error checking code here

      data = JSON.parse body
      res.send "#{data.gif}"

This one is working

module.exports = (robot) ->

 robot.respond /flip/i, (res) ->
  robot.http("http://www.tableflipper.com/json")
    .header('Accept', 'application/json')
    .get() (err, response, body) ->
      # error checking code here

      data = JSON.parse body
      res.send "#{data.gif}"