0
// accept a callback function to execute after getting results...
function searchCoords(callback){
  var result = result;
  connection.query('SELECT * FROM monitoring', function(err, result){
    if(err){
      console.log(err);
    }
    // run the callback function, passing the results...
    callback({result: result});
  });
}

// call like this...
searchCoords(function(resultsObject){
    console.log(resultsObject.result)
});

That's my code, I have an anonymous nested function which returns a variable to the outside by using a callback function. However, the variable returned (result) is an array which i need to use with googlemaps api in node.js. how can i turn this:

searchCoords(function(resultsObject){
   console.log(resultsObject.result)
});

into a variable that will contain the whole array so i can call it from themarkers option in the api

arrigonfr
  • 742
  • 3
  • 12
  • 34

2 Answers2

0

Instead of passing a callback to the function why not pass an array you can append to?

function start() {
    var myCoords = [];
    searchCoords(myCoords);
    otherFunction(myCoords); // This function does stuff with myCoords

}

function searchCoords(myCoords){
  connection.query('SELECT * FROM monitoring', function(err, result){
    if(err){
      console.log(err);
    }
    else {
      myCoords.push({result: result});
    }
  });
}
jeff_kile
  • 1,805
  • 16
  • 21
  • it doesn't change a thing, if i try to access it from the outside it still returns undefined or just says it doesn't exist. i have to try and call the array from the outside so i can run it through another function that will convert the coordinates to the right format so i can asing it to google maps maskers – arrigonfr Feb 05 '14 at 19:18
  • What do you mean by "outside"? In my example myCoords only exists in start and searchCoords. If you want it to exist someplace else you would need to pass it there or move it to a higher scope. I think the easy answer your looking for is to make it global, which you could do be defining it outside of a method or on window.myCoords but that would be bad programming practice and is ill advised, so you should really explain what "outside" is so that we can create the proper scope for the array. – jeff_kile Feb 05 '14 at 21:19
  • by outside i mean calling it from another function by just using the name of the function and the name of the variable like searchCoords.result for instance. – arrigonfr Feb 06 '14 at 17:57
  • what i need to do with the result array is pass it through other functions to convert some of the fields, so i can then call the converted variables from the google maps api markers option – arrigonfr Feb 06 '14 at 17:58
  • I just edited my answer to include "another function" named "otherFunction" I hope that answers your question, if not you need to show us more code. – jeff_kile Feb 06 '14 at 18:33
  • actually, with "push" what i'm doing is renaming the array by adding result to myCoords. what i need to do is access result from another function entirely, so i can turn it into an array and pass it to google maps marker – arrigonfr Feb 06 '14 at 20:44
  • I think you are confused about variable scope see this question: http://stackoverflow.com/questions/500431/javascript-variable-scope – jeff_kile Feb 07 '14 at 01:44
0

ok, this would be the code right now:

function searchCoords(myCoords){
      var result = result;
      connection.query('SELECT * FROM monitoring', function(err, result){
        if(err){
          console.log(err);
        }
          myCoords.push({result: result});
      });
}

which i call from main.js like this:
function start() {
    var myCoords = {};
    myCoords = database.searchCoords(myCoords);
    console.log(myCoords);
    //otherFunction(myCoords); // This function does stuff with myCoords
}

this is close to the solution i'm trying to get, but still doesn't work.. instead, console shows

TypeError: Object #<Object> has no method 'push'

I need a simple function to take the result given by the anonymous nested function and turn it into a variable that i can manipulate more easily, so i can add it to the marker param in google maps api

arrigonfr
  • 742
  • 3
  • 12
  • 34
  • have you solved it? – ThunderWiring Apr 14 '17 at 21:29
  • Yeah, but that was a long time ago... I can't even remember why I had that problem anymore... But any way, you could return te callback as callback({result: result}), or just callback(result)... And call use the result from your callback like this resultsObject.result, or like that... resultsObject (it depends on whether you return an object or just a different type of variable. – arrigonfr Apr 28 '17 at 19:16