[英]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.