【发布时间】:2016-04-29 03:12:24
【问题描述】:
我是个新手,如果这是一个愚蠢的问题,我很抱歉。
我正在编写一个 Hubot 脚本。我不明白我做错了什么。我需要一些帮助。我正在尝试从 MLB 获得分数。
我有这个……
module.exports = (robot) ->
robot.hear /score/i, (msg) ->
url = 'http://mlb.mlb.com/gdcross/components/game/mlb/year_2016/month_03/day_12/master_scoreboard.json'
msg.send(getData(url))
getData = (url) ->
robot.http(url)
.get() (err, res, body) ->
result = JSON.parse(body)
console.log(result.data.games.game[1].home_team_city)
team = result.data.games.game[1].home_team_city
当我运行上述命令时,console.log 语句打印“波士顿”,但机器人打印“[对象对象]”我如何让机器人打印“波士顿”。注意:我打算将 getData 函数重新用于一堆其他响应。
感谢您的帮助。
【问题讨论】:
-
(1) 你确定
.get() (err, res, body) ->是对的吗?您通常不会将回调传递给get,而不是像您一样说get()(callback_function)吗? (2) CoffeeScript 函数返回它们最后一个表达式的值,这可能就是[Object Object]的来源。 -
@muistooshort 但是最后一个语句是team,和控制台日志一样。从 hubot 文档来看,该语法是正确的。
标签: coffeescript hubot