title: Node.js 'undefined' variable when passed to function
body: <p>I am using the 'clouddns' module to import ~800 domain names into a Rackspace account. I keep getting an error saying the following</p>
  
  <pre><code>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.&lt;anonymous&gt; (/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.&lt;anonymous&gt; (/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
  </code></pre>
  
  <p>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. Here is the code:</p>
  
  <pre><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 &gt; 1) &amp;&amp; (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);
  }
  </code></pre>
  
  <p>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'. Can anyone shed some light on why this is happening?</p>