0

have u got an idea how can i take out RESULTS outside od CONNECT function?

var bike;
var mysql = require('mysql');
var con = mysql.createConnection({
    host: "localhost",    // ip address of server running mysql
    user: "root",    // user name to your mysql database
    password: "", // corresponding password
    database: "sociale" // use the specified database
});

// make to connection to the database.
con.connect(function(err) {
    if (err) throw err;
    // if connection is successful
    con.query("SELECT * FROM bikes", function (err, result, fields) {
        // if any error while executing above query, throw error
        if (err) throw err;
        // if there is no error, you have the result
        bike = result;
        console.log(bike);//WORKS
    });
    console.log(bike);//DOESNT WORK
});

I tried making global variable but it doesn't work outside of query function

  • The variable is not `undefined` outside the function. It's undefined **when** you use it. It's a time problem. Why do you want to use it outside the function? – jabaa Jun 05 '23 at 23:41

0 Answers0