简体   繁体   English

传递给函数时Node.js'undefined'变量

[英]Node.js 'undefined' variable when passed to function

I am using the 'clouddns' module to import ~800 domain names into a Rackspace account. 我使用'clouddns'模块将~800个域名导入Rackspace帐户。 I keep getting an error saying the following 我一直收到错误说下面的内容

TypeError: Cannot call method 'forEach' of undefined
at _wrapDomains (/home/duet/www/git/node-rackspace/node_modules/clouddns/lib/clouddns/core.js:146:17)
at /home/duet/www/git/node-rackspace/node_modules/clouddns/lib/clouddns/core.js:209:14
at Request._callback (/home/duet/www/git/node-rackspace/node_modules/clouddns/lib/clouddns/common.js:170:5)
at Request.self.callback (/home/duet/www/git/node-rackspace/node_modules/clouddns/node_modules/request/main.js:120:22)
at Request.EventEmitter.emit (events.js:98:17)
at Request.<anonymous> (/home/duet/www/git/node-rackspace/node_modules/clouddns/node_modules/request/main.js:555:16)
at Request.EventEmitter.emit (events.js:95:17)
at IncomingMessage.<anonymous> (/home/duet/www/git/node-rackspace/node_modules/clouddns/node_modules/request/main.js:517:14)
at IncomingMessage.EventEmitter.emit (events.js:117:20)
at _stream_readable.js:920:16

After looking at the library file in question (core.js) I decided to throw a couple log statements in there to see if I could figure out what was happening. 在查看有问题的库文件(core.js)后,我决定在那里抛出几个日志语句,看看我是否能弄清楚发生了什么。 Here is the code: 这是代码:

CloudDNS.prototype.getDomains = function getDomains(options, callback) {
    var args = Array.prototype.slice.call(arguments),
        callback = args[args.length - 1];

    if (typeof callback !== 'function') {
        throw new Error("This method requires a callback");
    }

    var self = this;
    var reqOpts = {
        method: 'GET',
        uri: this.dnsUrl('domains'),
        client: this
    }

    if ((arguments.length > 1) && (typeof arguments[0] === 'object')) {
        reqOpts.params = {
            name: args[0]
        }
    }

    this.rackspace(reqOpts, callback, function(body) {
        var result = JSON.parse(body);
        console.log(result.domains); //good here, it's an array and I can even forEach on it!
        self._wrapDomains(result.domains, callback); //undefined wtf?
        console.log(result.domains); //same as before, works brilliantly
    });
};

CloudDNS.prototype._wrapDomains = function _wrapDomains(domainArray, callback) {
    var self = this;
    var results = [];

    console.log(domainArray); //reports undefined

    domainArray.forEach(function(domain) {
        results.push(new(clouddns.Domain)(self, domain));
    });

    return callback(null, results);
}

I am weirded out by the fact that result.domains is defined before and after the method gets called, but inside that method it is 'undefined'. 我调查结果是在调用方法之前和之后定义了result.domains,但在该方法中它是'undefined'。 Can anyone shed some light on why this is happening? 任何人都可以解释为什么会这样吗?

I just checkout the source code on github. 我只是检查github上的源代码。 If you look at the second line in your stack trace, it's at /home/duet/www/git/node-rackspace/node_modules/clouddns/lib/clouddns/core.js:209:14 如果你查看堆栈跟踪中的第二行,它at /home/duet/www/git/node-rackspace/node_modules/clouddns/lib/clouddns/core.js:209:14

So _wrapDomains is getting called towards the end of the file in the createDomain method (on line 209). 因此,_wrapDomains在createDomain方法中被调用到文件的末尾(在第209行)。 If you investigate there, you will find out why it's calling it with null values. 如果你在那里调查,你会发现它为什么用空值来调用它。

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

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