你好,我正在尝试为hubot编写插件脚本。但是,我似乎有一个作用域问题。fork回调函数中的"msg"引用未定义,我似乎在回调函数中没有获取到任何变量。
我也遇到了与内置的jira-issues.coffee相同的问题!昨天在那个脚本中,回调函数中的jiraUrl未定义。今天早上它又恢复正常了,但现在它又停止工作了(在我重启hubot之后)。我甚至从未修改过它。
有人经历过类似的情况吗?
githubApi = require("node-github")
module.exports = (robot) ->
github = new githubApi { "version": "3.0.0" }
github.authenticate { "type":"basic", "username":process.env.HUBOT_GITHUB_USER, "password":process.env.HUBOT_GITHUB_P
ASSWORD }
robot.respond /kickstart\s(\S+)/i, (msg) ->
name = msg.match[1]
msg.send name
base_url = process.env.HUBOT_GITHUB_API || 'https://api.github.com'
github.repos.fork {"user":"Raven", "repo":"Raven","org":"codedemos"}, (err,data) ->
if err
msg.send "error :("
else
msg.send "Fork Complete..."
它编译成
// Generated by CoffeeScript 1.6.3
(function() {
var githubApi;
githubApi = require("node-github");
module.exports = function(robot) {
var github;
github = new githubApi({
"version": "3.0.0"
});
github.authenticate({
"type": "basic",
"username": process.env.HUBOT_GITHUB_USER,
"password": process.env.HUBOT_GITHUB_PASSWORD
});
return robot.respond(/kickstart\s(\S+)/i, function(msg) {
var base_url, name;
name = msg.match[1];
msg.send(name);
base_url = process.env.HUBOT_GITHUB_API || 'https://api.github.com';
return github.repos.fork({
"user": "Raven",
"repo": "Raven",
"org": "codedemos"
}, function(err, data) {
var id;
if (err) {
return msg.send("error :(");
} else {
return id = data.id;
}
});
});
};
msg.send("Fork Complete...");
}).call(this);
看起来没问题,但运行它会产生以下结果:
ReferenceError: msg is not defined
at Object.<anonymous> (/opt/kbot/scripts/kickstart.coffee:48:2, <js>:36:3)
at Object.<anonymous> (/opt/kbot/scripts/kickstart.coffee:33:1, <js>:38:4)
at Module._compile (module.js:456:26)