0

I am new in node, I wanna return result from mysql query as array.

Here my code :

function alterObject(obj) {

  var c = [];
  con.query("SELECT KeyName FROM SearchKeyWord", function(err, rows, field) {
    for (var i in rows) {
      c = c + rows[i].KeyName + "'" + "," + "'";
    }
    c = c.substring(0, c.length - 2);
    console.log(c)
  });

  obj.foo = c;

}

alterObject(myObj);

console.log(myObj.foo);
t.niese
  • 39,256
  • 9
  • 74
  • 101
allord
  • 1
  • 1
  • 1
    When you ask question you should tell what your actual problem is, what you expect the result should be and what you actually get. – t.niese Aug 07 '17 at 04:22
  • `query` as many functions in nodejs, is an asynchronous function. The callback you pass to asynchronous functions is called when the result is ready and will always be called, after the current _code flow_ is finished, so at the time you do `obj.foo = c;` or `console.log(myObj.foo);` the callback was not called. – t.niese Aug 07 '17 at 04:25
  • Another related duplicate [Why is my variable unaltered after I modify it inside of a function? - Asynchronous code reference](https://stackoverflow.com/questions/23667086) – t.niese Aug 07 '17 at 04:27

0 Answers0