gpt4 book ai didi

javascript - 将 Node-cron 与 CoffeeScript 结合使用

转载 作者:太空宇宙 更新时间:2023-11-04 00:47:57 25 4
gpt4 key购买 nike

我正在尝试为 hubot 编写一个使用 node-cron 的 Coffeescript 脚本。我找到了一个像这样的例子,它效果很好:

module.exports = (robot) ->
cronJob = require('cron').CronJob
new cronJob('0 */1 * * * *', everyMinute(robot), null, true)

everyMinute = (robot) ->
-> robot.messageRoom '#billing', 'hey brah!'

但是,这是我当前的脚本。它似乎永远不会触发 Node 事件。我基本上试图每分钟检查一次远程 API 的状态并将结果设置在 hubot 的大脑中。我在这里做错了什么?这基本上是我写的第一个 CoffeeScript ,所以仍然可以找到我的脚。

module.exports = (robot) ->

cronJob = require('cron').CronJob
new cronJob('0 */1 * * * *', getOnPoint, null, true)

getOnPoint = ->
robot.http("https://#{pagerDutyDomain}.pagerduty.com/api/v1/schedules/#{pagerDutyService}")
.headers(Authorization: "Token token=\"#{pagerDutyToken}\"", Accept: 'application/json')
.query(since: '2015-11-23T21:15:49Z', until: '2015-11-23T22:15:49Z')
.get() (err,res,body) ->
if res.statusCode isnt 200
msg.send "Request didn't come back HTTP 200 :( got back #{body}"
return

json = JSON.parse(body)
onpoint_person = json.schedule.final_schedule.rendered_schedule_entries[0].user.email
robot.logger.debug "Setting onpoint to #{onpoint_person}"
robot.brain.set 'onpoint_person', onpoint_person.split("@")[0]

我查看了coffeescript编译的JS,很明显我声明函数的方式很重要,但我不知道为什么。

非常感谢任何帮助。

最佳答案

我也遇到了同样的问题,如果你注意到他的方法中有两个 ->,我通过在 cronJob 和你想要使用的方法之间创建另一种方法来解决这个问题。

new cronJob('0 */1 * * * *', getOnPointMethod(), null, true)

getOnPointMethod = ->
-> getOnPoint()

这会起作用,不知道它是多么优雅或多好的解决方案,但它确实可以完成工作。

关于javascript - 将 Node-cron 与 CoffeeScript 结合使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33882939/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com