简体   繁体   English

Hubot中的Coffeescript错误“未定义不是函数”

[英]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: 我正在调用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

but it returns ERROR TypeError: undefined is not a function in the console. 但返回ERROR TypeError: undefined is not a function控制台中ERROR TypeError: undefined is not a function What's wrong with this? 这怎么了

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() 我相信这是失败的原因是因为你使用的res在的地方msg ,然后res的功能的情况下再次.get()

I'm guessing the error is on this line: 我猜错误是在这条线上:

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

Instead of passing the callback as an argument to get , you're calling get with no arguments and then trying to call the result (which is undefined ) as though it were a function. 而不是将回调作为get的参数传递,而是在不带参数的情况下调用get ,然后尝试将结果( undefined )当作函数来调用。 My CoffeeScript is rusty, but I think you want this instead: 我的CoffeeScript很生锈,但是我想您要这样:

.get (err, res, body) ->

It might be incompatible version of installed hubot and documentation but I found out the res from an http method has no send, but the one from the /hear command does. 它可能是已安装的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

This should work, but either the official documentation is wrong, either the default installation of hubot is outdated. 这应该可以,但是官方文档有误,或者hubot的默认安装已过时。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM