在hubot中Coffeescript错误“未定义不是函数”

时间:2015-06-30 06:15:48

标签: coffeescript hubot

我正在调用我的django应用程序返回一个JSON对象,我正在使用以下代码来执行此操作:

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

但它在控制台中返回ERROR TypeError: undefined is not a function。这有什么问题?

3 个答案:

答案 0 :(得分:2)

应该是这样的:

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

我认为失败的原因是因为您在res的位置使用msg,然后在函数res的上下文中再次使用.get()

答案 1 :(得分:0)

我猜错误就在这一行:

.get() (err, res, body) ->

不是将回调作为参数传递给get,而是在没有参数的情况下调用get,然后尝试调用结果(undefined),就好像它是一个功能。我的CoffeeScript很生疏,但我想你想要这个:

.get (err, res, body) ->

答案 2 :(得分:0)

它可能是已安装的hubot和文档的不兼容版本,但我发现http方法的res没有发送,但是来自/ hear命令的res。

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

这应该可行,但是官方文档是错误的,要么hubot的默认安装已经过时了。