[英]Coffeescript error “undefined is not a function” in 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
控制台中ERROR TypeError: undefined is not a function
。 这怎么了
应该是这样的:
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()
我猜错误是在这条线上:
.get() (err, res, body) ->
而不是将回调作为get
的参数传递,而是在不带参数的情况下调用get
,然后尝试将结果( undefined
)当作函数来调用。 我的CoffeeScript很生锈,但是我想您要这样:
.get (err, res, body) ->
它可能是已安装的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的默认安装已过时。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.