6

我正在使用“clouddns”模块将大约 800 个域名导入 Rackspace 帐户。我不断收到一条错误消息,说以下内容

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

在查看了有问题的库文件(core.js)之后,我决定在其中抛出几个日志语句,看看我是否能弄清楚发生了什么。这是代码:

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

我对 result.domains 在方法被调用之前和之后定义的事实感到奇怪,但在该方法内部它是“未定义的”。任何人都可以阐明为什么会这样吗?

4

1 回答 1

1

我只是在 github 上查看源代码。如果您查看堆栈跟踪中的第二行,它是at /home/duet/www/git/node-rackspace/node_modules/clouddns/lib/clouddns/core.js:209:14

所以 _wrapDomains 在方法中的文件末尾被调用createDomain(在第 209 行)。如果你在那里调查,你会发现为什么它用空值调用它。

于 2013-09-30T15:54:59.803 回答