How to save connection result in a variable in Node.js ? Last Updated : 07 Apr, 2021 Comments Improve Suggest changes Like Article Like Report We are going to use the query function in MySQL library in node.js that will return our output as expected. Using this approach, we can save connection result in a variable in Node.js. Setting up environment and Execution: Step 1: Initialize node project using the following command. npm init Step 2: Turn on MySQL Database Step 3: After creating the NodeJS application, Install the mysql module using the following command. npm install mysql Database Table: Our sample gfg_table database with look like this. Step 4: Create an index.js file with the following code. index.js const mysql = require("mysql"); var db_con = mysql.createConnection({ host: "localhost", user: "root", password: "", database: "gfg_db" }); let output; const setOutput = (rows) => { output = rows; console.log(output); } db_con.connect(async(err) => { if (err) { console.log("Database Connection Failed !!!", err); return; } console.log("Connected to Database"); let query = 'SELECT * FROM users'; db_con.query(query, (err, rows) => { if (err) { console.log("internal error", err); return; } // This is the important function setOutput(rows); }); }); Step 5: Run the index.js file using the following command. node index.js Output: Comment More infoAdvertise with us Next Article How to save connection result in a variable in Node.js ? P pratikraut0000 Follow Improve Article Tags : Web Technologies Node.js Node.js-Methods NodeJS-Questions NodeJS-MySQL +1 More Similar Reads How to Properly Reuse Connection to MongoDB Across NodeJs Efficiently managing database connections is important for Node.js applications using MongoDB. MongoDB connection reuse in Node.js is a practice that optimizes resource utilization and enhances application performance. By reusing connections, developers can reduce the overhead associated with establ 6 min read How to Use Connection Pooling with MySQL in Node.js? MySQL is one of the most preferred relational databases, While Node.js is another name for JavaScript runtime environment. While assessing a large number of connections in the database in a Node. In this regard, effectiveness in managing them is also a significant determinant when developing and mai 3 min read How to Push Data to Array Asynchronously & Save It in Node.js ? Node.js is a powerful environment for building scalable and efficient applications, and handling asynchronous operations is a key part of its design. A common requirement is to collect data asynchronously and store it in an array, followed by saving this data persistently, for instance, in a databas 7 min read How to connect mongodb Server with Node.js ? mongodb.connect() method is the method of the MongoDB module of the Node.js which is used to connect the database with our Node.js Application. This is an asynchronous method of the MongoDB module.Syntax:mongodb.connect(path,callbackfunction)Parameters: This method accept two parameters as mentioned 1 min read How to Pass/Access Node.js Variable to an HTML file/template ? When building web applications with Node.js, itâs often necessary to pass server-side variables to the client-side for rendering dynamic content. This can be achieved using various techniques and templating engines. This article will guide you through different methods to pass and access Node.js var 2 min read How to Connect to Telnet Server from Node.js ? Connecting to a Telnet server from Node.js involves creating a client that can communicate using the Telnet protocol. Telnet is a protocol used to establish a connection to a remote host to execute commands and transfer data. In Node.js, you can use various packages to facilitate the connection to a 4 min read How to establish connection between Node.js ans Redis ? NodeJS is an open-source back-end JavaScript runtime environment for executing JavaScript outside the browser. It is widely used for server-side development in many companies. Redis is a popular in-memory key-value database. Unlike traditional databases that run on a computer's hard disk and store 3 min read What is Connection Pooling with MongoDB in Node.js ? In this article, we are going to see in-depth connection pools MongoDB in Nodejs. A connection pool serves as a collection of established TCP connections that your application's MongoDB driver maintains with a MongoDB server. This pool's purpose is to minimize the need for frequent network handshake 2 min read How to use Async-Await pattern in example in Node.js ? Modern javascript brings with it the async-await feature which enables developers to write asynchronous code in a way that looks and feels synchronous. This helps to remove many of the problems with nesting that promises have, and as a bonus can make asynchronous code much easier to read and write. 1 min read Reading Environment Variables From Node.js Environment Variable: The two fundamental concepts of any programming language are variables and constants. As we know that constants and variables both represent the unique memory locations that contain data the program uses in its calculations. The variable which exists outside your code is a part 2 min read Like