Search code examples
coffeescripthubot

Coffeescript error "undefined is not a function" in hubot


I'm making call to my django app that returns a JSON object and I'm using the following code to do so:

robot.hear /lunch for today/i, (res) ->
    robot.http("http://my_ip_address/show")
        .header('Accept', 'application/json')
        .get() (err, res, body) ->
            data = JSON.parse body
            res.send data.food

but it returns ERROR TypeError: undefined is not a function in the console. What's wrong with this?


Solution

  • is should look like this:

    module.exports= (robot) ->
      robot.hear /lunch for today/i, (msg) ->
        robot.http("http://my_ip_address/show")
          .header('Accept', 'application/json')
          .get() (err, res, body) ->
            console.log res.statusCode
            data = JSON.parse body
            msg.send data.food
    

    I believe the reason it is failing is because you are using res in the place of msg and then res again in the context of the function .get()