Node.js 'undefined' when passing a function

I use the clouddns module to import ~ 800 domain names into a Rackspace account. I keep getting error message

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 (core.js), I decided to drop a couple of log reports to see if I could figure out what was going on. 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 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); } 

What amazes me is the fact that result.domains is defined before and after the method call, but inside this method it is "undefined". Can anyone shed some light on why this is happening?

+6
source share
1 answer

I am just checking the source code on github. If you look at the second line in the stack trace, then it is at /home/duet/www/git/node-rackspace/node_modules/clouddns/lib/clouddns/core.js:209:14

So _wrapDomains gets called at the end of the file in the createDomain method (on line 209). If you research there, you will find out why it calls it with zero values.

+1
source

Source: https://habr.com/ru/post/954079/


All Articles