简体   繁体   English

Coffeescript回调范围问题

[英]Coffeescript callback scoping issue

Hi i'm trying to write a addon script for hubot. 嗨,我正在尝试为hubot编写一个插件脚本。 I seem to have a problem with scoping. 我似乎有一个范围问题。 The reference of "msg" inside the fork callback is undefined, i don't seem to get any variables in the callback. fork回调中“msg”的引用是未定义的,我似乎没有得到回调中的任何变量。

I've also had this same issue with the built-in jira-issues.coffee! 我内置的jira-issues.coffee也有同样的问题! Yesterday in that script the jiraUrl was undefined in the callback. 昨天在该脚本中,jiraUrl在回调中未定义。 This morning it was working again and now it's stopped working again (after i restarted hubot). 今天早上它再次工作,现在它再次停止工作(重新启动hubot之后)。 I've never even modified it. 我从来没有修改它。

Has anyone experienced anything like this? 有没有人经历过这样的事情?

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..."

it compiles to 它编译成

// 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);

Which looks fine yet running it yields: 哪个看起来不错但运行它会产生:

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)

It's almost certainly a whitespace problem with that last line. 这几乎肯定是最后一行的空白问题。 Triple-check you are using consistent indentation in the whole file and make sure your file has a final newline after msg.send "Fork Complete..." . 三重检查您在整个文件中使用一致的缩进,并确保您的文件在msg.send "Fork Complete..."之后有一个最终换行符。 I copied and pasted your coffeescript code and compiled it to javascript and the last msg.send line gets correctly put inside the proper function, vs your js above which has that line in the wrong place. 我复制并粘贴您的CoffeeScript代码,并将其编译为JavaScript和最后msg.send线被正确地把inside的正常功能,您VS爵士高于该有错误的地方该行。

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

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