0

i'm currently working with electron and I need to get some data from a mysql database, what is working good.

But there is a problem with sending values between functions. I'm kinda new to JS and electron, so maybe i just missed something:

function getUserData(user, data){

// connect
var connection = connect();

// Perform a query
$query = 'SELECT ' + data + ' FROM `users` WHERE user_id = ' + user;

// Query ausführen
connection.query($query, function(err, rows, fields) {
    if(err){
        console.log("There was an error with executing the query.");
        console.log(err);
        return;
    }

    return rows;
});

// End function
return;
}

So basically i want to return "rows", but it says it's undefined. Right above

return rows;

if i check with

console.log(rows);

it has data in it

Am i missing something?

Thanks in advance!

Michi
  • 41
  • 7
  • You return undefined from your function. Also I am not familiar with that api but you probably want to return connection.query(.....). Latestly, that function is vulnerable to sql injection attacks. – terpinmd Jul 07 '17 at 22:11

1 Answers1

0

Well, you actually return undefined with return;.

If it (connection.query) is synchronous then just do return connection.query(/*smth*/). If it is asynchronous you won't be able to do that this easily and the best you can do is, in the function provided as argument, change the value of a variable that is globally defined (or return the Promise/"asynchronous handling" object if there's one).

Vivick
  • 3,434
  • 2
  • 12
  • 25