title: nodejs, mysql, async itterative functions
body: <p>I'm having a bit of trouble with an itterative function in nodejs. </p>
  
  <p>I'm stepping through an object and checking if that object has any sub-objects attached (think: a star has a planet has a moon has an orbital station has a ship).</p>
  
  <p>I'm trying to assemble this all into a nice array of objects to push to the client.</p>
  
  <p>Here's the function:</p>
  
  <pre><code>        var subNodeProc = function(nodeList,sqlP,itteration_count) {
              var async = require('async');
  
              --itteration_count;
              async.each(nodeList,function(dd,cb){
  
                      var simple = {
                          sql:sqlP,
                          values:[dd.node_id],
                          timeout:40000
                      };
  
                      dd.subnodes = false;
                      connection.query(simple, function(err,rslt){
                          if (err) {
                              cb(err);
                          } else {
                              if (rslt.length &gt; 0) {
  
                                  var r = nodeList.indexOf(dd);
                                  if (itteration_count &gt; 0) {
                                      rslt = subNodeProc(rslt,sqlP,itteration_count);
                                  }
                                  nodeList[r].subnodes = rslt;
  
                              }
                              cb();
                          }
                      });
  
              },function(err){
  
                  if (err) {
                      return err;
                  } else {
                      return nodeList;
                  }
  
              });
  
          }
  </code></pre>
  
  <p>When I trigger the function it returns a nodelist of undefined. Can anyone give me a pointer in the right direction? I can't get it to work</p>
  
  <p>Thanks!</p>
  
  <p>Edit: here's a sample of the data I'm itterating over:</p>
  
  <p>The SQL statement:</p>
  
  <pre><code>SELECT n.id as node_id, n.name, n.system_id, n.parent_id as parent_id FROM nodes as n WHERE n.parent_id = ?
  </code></pre>
  
  <p>Sample nodeList for input:</p>
  
  <pre><code>[ { node_id: 1,
      name: 'Planet A',
      system_id: 1,
      parent_id: null,
  },
  { node_id: 2,
      name: 'Moon',
      system_id: 1,
      parent_id: 1,
  },
  { node_id: 3,
      name: 'Debris',
      system_id: 1,
      parent_id: 2,
  },
  { node_id: 4,
      name: 'Asteroid',
      system_id: 1,
      parent_id: 1,
  } ]
  </code></pre>
  
  <p>Moon A has a parent_id of 1 and node_id of 2, moon A also has a ship (ship A, node_id:3, parent_id:2) orbiting it.</p>
  
  <p>What I want :</p>
  
  <pre><code>[ { node_id: 1,
      name: 'Planet A',
      system_id: 1,
      parent_id: null,
      subnodes:[{
          node_id: 2,
          name: 'Moon A',
          system_id: 1,
          parent_id: 1,
          subnodes: [{
              node_id:3,
              name: 'Ship A',
              system_id:1,
              parent_id:2
          },
          {...}]
      },
      {...}]
  },
  {...}]
  </code></pre>