簡體   English   中英

Coffeescript回調范圍問題

[英]Coffeescript callback scoping issue

嗨,我正在嘗試為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)

這幾乎肯定是最后一行的空白問題。 三重檢查您在整個文件中使用一致的縮進,並確保您的文件在msg.send "Fork Complete..."之后有一個最終換行符。 我復制並粘貼您的CoffeeScript代碼,並將其編譯為JavaScript和最后msg.send線被正確地把inside的正常功能,您VS爵士高於該有錯誤的地方該行。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM