The connection query is happening asynchronously and is non-blocking (the query is started but not completed before it moves to the next line of code), so return returnresult still has the value ‘test value’ when it is returned.
So, how can you make it wait for something that is still yet to happen, i.e. something that is going to happen in the future?
Meteor.methods({
'mysqltestcall1': function(){
// use the node fibers npm
var Future = Npm.require('fibers/future');
// make some new future
var fut = new Future();
connection.query('SELECT * FROM players', function(err, rows, fields) {
if (err) throw err;
console.log(rows);
// when this callback eventually happen, returns rows
fut.return(rows);
});
// wait for something from the future
return fut.wait();
}
});