【问题标题】:Unable to access local scope of an asynch method from an inner function with node.js无法使用 node.js 从内部函数访问异步方法的本地范围
【发布时间】:2016-03-08 13:33:11
【问题描述】:

我在使用这个小应用程序时遇到了问题。从我的方法“innerFunc”中,为“self.words”中的每个单词声明并执行一次,我想访问“outerFunc”的本地范围。

我需要访问“item”,每个“innerFunc”声明都必须不同。

var self = this;
self.words = ["a", "b", "c"];

async.each(self.words,
          function outerFunc (item, callback) {
                var item = item;
                self.item = item;
                var innerFunc = function (error, response, body) {
                    console.log(self.words); //I can access to it.
                    console.log(self.item); //I can access it.
                    console.log(item); //Not accesible -> This is the problem
                    callback(null);
                };

                request("http://blablabla", innerFunc);

            },
          function(err){
            //Do nothing.
          }
        );

问题是我可以访问“self”,这是一个在全局范围内声明的变量。但不是在声明“innerFunc”时在本地范围内声明的“项目”。我该怎么做?

另外,如果您知道实现此异步任务链的更好模式,欢迎提出建议。

【问题讨论】:

  • 你能定义“不可访问”吗?
  • 当你尝试访问item时,你得到undefined吗?它会出错吗? self.item 和 item 一样吗?
  • @robertklep @Compynerd255 chrome 开发者工具说的是:item: <not available> 而self.item 对于所有迭代都是“c”。

标签: javascript node.js express closures


【解决方案1】:

最佳做法是使用不同的函数来返回您想要的函数,以便每次固定不同的“项目”。 试试这个代码:

var self = this;
self.words = ["a", "b", "c"];

var retInnerFunc = function(item) {
        return function(error, response, body) {
                console.log(self.words);
                console.log(self.item);
                console.log(item); 
                callback(null);
            };
};


async.each(self.words,
      function outerFunc (item, callback) {
            var innerItem = item;
            self.item = item;

            request("http://blablabla", retInnerFunc(innerItem));

        },
      function(err){
        //Do nothing.
      }
    );

我还为内部变量更改了名称“item”,我认为这不是最好的命名,使用与参数相同的名称...

【讨论】:

  • 谢谢你,我会听从你的建议的。但这不是解决我的错误的原因。我将发布另一个答案来解决我的问题。
【解决方案2】:

好吧,我终于解决了我的问题。事实证明,我的解决方案是正确的,尽管可能不是如何构建代码的最佳示例。

问题是为了调试我的代码,我使用的是 Node Inspector v0.12.3。它提供了与 WebKit Web Inspector 相同的界面。

当我编写我的应用程序时,为了以更快的方式测试我的闭包能力,我没有在我的方法中填充对 item 的引用。我在函数内部停止了调试器,并尝试使用 Watch Expression 来查看 item 是否可见。

我的代码是这样的:

...
var item = whatever; //Different value on each iteration.
myFunc = function(error, response, body) {
                console.log(self.words); 
                //stopping debugger and looking for "item" value using Watch Expression
 };
...

Watch Expression 的输出是item: <not available>

当我改用正确的方法时:

...
    var item = whatever; //Different value on each iteration.
    myFunc = function(error, response, body) {
                    console.log(self.words);
                    console.log(item);
                    //stopping debugger and looking for item using Watch Expression
     };
...

现在 Watch Expression 给了我正确的值item: whatever。

当您不在方法中使用此类变量时,WebKit Web Inspector(或 Node Inspector)似乎不提供对它们的访问,即使它们应该是可访问的。这是一个既愚蠢又棘手的问题。

如果有人对这个调试工具为什么会发生这种情况有更准确的解释,请分享你的想法:)

【讨论】:

    猜你喜欢
    • 2014-02-07
    • 1970-01-01
    • 2021-04-04
    • 1970-01-01
    • 1970-01-01
    • 2013-06-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多