javascript - NodeJs program not exiting after completion - Stack Overflow

Sorry for this noob question, I'm beginner in Javascript. I am using the NodeJs MySQL package to c

Sorry for this noob question, I'm beginner in Javascript. I am using the NodeJs MySQL package to connect my node application to my database. But after running the query successfully the program doesn't exit it stays there forever until I terminate it manually. I want the program to run the query and exit the program.

The code that I'm using:

var mysql = require("mysql");

var pool = mysql.createPool({
    connectionLimit: 100,
    host: "Hostname",
    user: "User",
    password: "Password",
    database: "DB_name",
    debug: false,
});

function sql_query() {
    pool.getConnection(function (err, connection) {
        if (err) {
            console.log(err.code + ' : '+ err.sqlMessage);
            return;
        }

        var query = "MY-QUERY_STRING";

        connection.query(query, (error, results, fields) => {

            connection.release();

            if (error) {
                console.log(error);
                return;
            }

            console.log("Done");
            return results;
        });
    });
}

var result = sql_query();
console.log(result);

Sorry for this noob question, I'm beginner in Javascript. I am using the NodeJs MySQL package to connect my node application to my database. But after running the query successfully the program doesn't exit it stays there forever until I terminate it manually. I want the program to run the query and exit the program.

The code that I'm using:

var mysql = require("mysql");

var pool = mysql.createPool({
    connectionLimit: 100,
    host: "Hostname",
    user: "User",
    password: "Password",
    database: "DB_name",
    debug: false,
});

function sql_query() {
    pool.getConnection(function (err, connection) {
        if (err) {
            console.log(err.code + ' : '+ err.sqlMessage);
            return;
        }

        var query = "MY-QUERY_STRING";

        connection.query(query, (error, results, fields) => {

            connection.release();

            if (error) {
                console.log(error);
                return;
            }

            console.log("Done");
            return results;
        });
    });
}

var result = sql_query();
console.log(result);
Share Improve this question asked May 23, 2020 at 18:29 darkpanda08darkpanda08 797 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 4

You have to close the MySQL connection to exit the program. by using the following code in the success block you will exit the program.

pool.end(function(err) {
  if (err) {
    return console.log('error:' + err.message);
  }
  console.log('Close the database connection.');
});

As long as your pool is open, the node process will not exit. Check this for further details. If you want to close your pool after executing one query you can do the following:

...
connection.query(query, (error, results, fields) => {
            pool.end(); // you can pass a callback here as well to handle errors
            ...
});
...

Also note that you're trying to assign the result of sql_query to result. This won't work as you can't return a value from a callback this way. Check this for further details.

You have to close the connection with pool.end(), in your case you want it to be before returning results.

        connection.query(query, (error, results, fields) => {

            connection.release();

            if (error) {
                console.log(error);
                return;
            }

            console.log("Done");
            pool.end()
            return results;
        });

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1742347512a4426845.html

相关推荐

  • javascript - NodeJs program not exiting after completion - Stack Overflow

    Sorry for this noob question, I'm beginner in Javascript. I am using the NodeJs MySQL package to c

    6小时前
    10

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信