gpt4 book ai didi

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

转载 作者:数据小太阳 更新时间:2023-10-29 05:36:16 27 4
gpt4 key购买 nike

我正在使用“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 是在调用该方法之前和之后定义的,但在该方法内部它是“未定义的”,这让我很奇怪。谁能阐明为什么会发生这种情况?

最佳答案

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

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

关于javascript - 传递给函数时的 Node.js 'undefined' 变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18876174/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com