0

I'm having trouble getting a return value from an Asynchronous function in JavaScript. I have tried a few different approaches based off of other posts found here - How to properly return a result from mysql with Node? and here Store mysql query rows in variable for later use. My current code is as follows .

function get_info(callback) {

    var mysql = require('mysql');
    var connection = mysql.createConnection({
        host: 'xxxxxxx',
        user: 'bob',
        password: 'bob',
        database: 'xxxxxx'
    });

    connection.connect(function (err) {

        if (err) throw err;
        connection.query("SELECT Name FROM webform", function (err, result) {
            if (err) throw err;
             return callback(result);
        });
        connection.end();
    });
}

 var newli = get_info(function(result){
    stuff_i_want = result;
   return stuff_i_want;
 });

console.log(newli);

I'm expecting the console.log(newli) to print the value of stuff_i_want which I'm returning from the get_info() function but I instead get undefined. I'm trying to get the value of result from the connection.query stored in a variable that I can use later.

franchyze923
  • 1,060
  • 2
  • 12
  • 38
  • 1
    Possible duplicate of [How do I return the response from an asynchronous call?](https://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call) – Patrick Roberts Dec 19 '18 at 17:34
  • Also see [Why is my variable unaltered after I modify it inside of a function? - Asynchronous code reference](https://stackoverflow.com/q/23667086/1541563). What you're trying to do, without realizing, is print a value _now_ that isn't actually available until sometime _in the future_. – Patrick Roberts Dec 19 '18 at 17:36
  • I took a look at the pages and they mention and demonstrate the use of callbacks which is what I believe I am trying to do in my code. – franchyze923 Dec 19 '18 at 17:56
  • The second link should explain why you _shouldn't_ do what you're currently doing. The `newli` will never be available in that scope, the callback function will be the only scope where it is available. – Patrick Roberts Dec 19 '18 at 17:58
  • From the second link, it appears what I'm trying to accomplish is not possible with just callbacks. The example the poster walks through is very similar to my code. The poster mentioned using promises which I'm looking into now. – franchyze923 Dec 19 '18 at 18:26
  • Using promises is just recommended because they're ultimately more flexible and modern than callbacks. But that is just one of the solutions to what you're trying to do. The general advice is _embrace the asynchronous behavior of JavaScript_, which translates to _don't fixate on making the variable available in the outer scope, rewrite your code so that the only scope it's available in can still be consumed by the rest of your code_. – Patrick Roberts Dec 19 '18 at 18:29
  • hmm, okay that gives me something to think about. I'll try to redesign my code to incorporate this. – franchyze923 Dec 19 '18 at 18:31
  • If you [edit] your question to include how you're trying to actually use `newli`, that would make this a separate question from the canonical duplicates which I've linked you to. – Patrick Roberts Dec 19 '18 at 18:33
  • I'm actually trying to connect a website built with Wix to an external DB. The more I think about it, I should be able to use the output within the get_info function..no need for it to return anything. I'm able to successfully do this locally with just Node but when I try to do it in the Wix environment I get Uncaught (in promise) Error: Bad Gateway. This is well out of the scope of the original question. Thank you for the help. – franchyze923 Dec 19 '18 at 18:50
  • Don't try to connect to your backend database directly from the client. All of the credentials you use client-side are then vulnerable and can be used to exploit your web application. – Patrick Roberts Dec 19 '18 at 18:51
  • Can you suggest an approach I should look into? – franchyze923 Dec 19 '18 at 18:54
  • Write a public API on your node server that is accessible via CORS from the wix domain, and make the calls to your external database from within the API routes. That way you're not exposing backend credentials to the client. – Patrick Roberts Dec 19 '18 at 18:56
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/185509/discussion-between-fpolig01-and-patrick-roberts). – franchyze923 Dec 19 '18 at 19:00

0 Answers0