0

我正在尝试使用 coffeeScript 发出 get 请求,但它甚至没有向我的 URL 发出请求:

module.exports = (robot) ->
  robot.respond /foo (.*) bar (.*) foobar (.*) /i, (msg) ->
    foo = msg.match[1]
    bar = msg.match[2]
    foobar = msg.match[3]
    robot.http("http://localhost:8000/a/")
      .query({
        'foo': foo
        'bar': bar
        'foobar': foobar
      })
      .get() (err, res, body) ->
        json = JSON.parse(body)
        msg.send(json)

当我使用浏览器发出相同的请求时,它可以工作:

http://localhost:8000/a/?foo=1&bar=2&foobar=3

我正在尝试运行hubot

hubot 1 bar 2 foobar 3
4

1 回答 1

2

你的 CoffeeScript 语法有点不对劲。您在get通话中的结构是这样的:

f() x

当你想要它是这样的:

f x

这部分:

.get() (err, res, body) ->
    json = JSON.parse(body)
    msg.send(json)

get不带参数调用,然后将get返回的任何内容作为函数调用,并(err, res, body) -> ...作为参数调用。大概您想将回调get作为参数传递给:

.get (err, res, body) ->
    json = JSON.parse(body)
    msg.send(json)
于 2014-03-04T06:49:53.693 回答