title: Javascript (node.js) value returned by function is undefined?
body: <p>I've got a function that returns a json with some data retrieved from a postgis database:</p>
  
  <pre><code>function ObtenerPuntosIniciales(TablaOrigen,callback)
      {  
          var conexion = "postgres://postgres:postgres2@"+ Server +"/" + BD;
          var sqlPunto='SELECT json FROM ' + TablaOrigen;
          pg.connect(conexion, function(err, client, done) {
              if(err) {
                  return callback(new Error('ObtenerPuntosIniciales_error en la conexion:', err));
              }
              client.query(sqlPunto, function(err, Resultado) {
                    if(err) {
                      return callback(new Error('ObtenerPuntosIniciales_error al ejecutar la consulta:', err));
                    }
                    return callback(null,Resultado);
                    done();
              });                                                                               
          });
      }
  </code></pre>
  
  <p>After that i've got a code snippet that assigns the value returned by the function to a variable but this is always undefined although inside the function the value is being displayed:</p>
  
  <pre><code>var ObtainedData = ObtenerPuntosIniciales(TablaOrigenDeLosPuntos,function ( err,result ) {                                                                                                                                 
          if (err) {
                    console.log('Error:' + err);
                    return;
          }
          console.log('FunctionData:' +result.rows[0].json);
          return result;
  });
  console.log('ObtainedData:'+ ObtainedData); 
  </code></pre>
  
  <p>The console.log inside the function show the right value.
  The console.log outside the function,on the other hand, shows undefined.</p>
  
  <p>Here is what the console is showing:</p>
  
  <pre><code>ObtainedData:undefined
  
  GET /?ServidorOrigenDeLosPuntos=localgis-modelo&amp;BDOrigenDeLosPuntos=geopista&amp;Car
  garCapas=true&amp;NombreCapa1AMostrar=Circuitos&amp;TablaCapa1=getxo_alum_circuitos_old&amp;
  TablaOrigenDeLosPuntos=getxo_alum_circuitos_old&amp;NombreCampoClave1OrigenPto=id&amp;Va
  lorBuscadoCampoClave1OrigenPto=3 304 421ms
  
  FunctionData:{"type":"LineString","coordinates":[[-3.009299817715526,43.3
  46455178899078],[-3.009294275536192,43.346457831419585]]}
  </code></pre>
  
  <p>I guess the problem is in the function, that its not returning the value into the variable, or could be an async problem.</p>