简体   繁体   中英

NodeJS return SQL queries to pass to another file

I have been struggling with the returning of data for the past 2 days. I really need help in getting the data to show in another .js file but I can't seem to do so.

From all the research I have done, I know that I need the callback function in order to do the return. When I output the data in file1.js , it shows, which is correct.

However, I need to access the returned data in my file2.js but it is not showing. Am I missing anything out? Please help, any response is greatly appreciated. Thanks.

Note that my return statement in file1.js is near the end of the code. Also, my "res" array is ALWAYS empty when accessed outside the function. Why is this so?

file1.js

 var sql = require("mssql"); // Create a configuration object for our Azure SQL connection parameters var dbConfig = { server: "***", // Use your SQL server name database: "***", // Database to connect to user: "***", // Use your username password: "***", // Use your password port: 1433, // Since we're on Windows Azure, we need to set the following options options: { encrypt: true } }; var obj = {}; var res = []; // This function connects to a SQL server, executes a SELECT statement, // and displays the results in the console. function getProducts(callback) { // Create connection instance var conn = new sql.ConnectionPool(dbConfig); conn.connect() // Successfull connection .then(function () { // Create request instance, passing in connection instance var req = new sql.Request(conn); // Call mssql's query method passing in params req.query("SELECT sp.product_name, count(ss.product_id) as 'quantity' " + "FROM smartcoolerstocks ss JOIN smartcoolerproducts sp " + "ON sp.product_id = ss.product_id " + "GROUP by sp.product_name ") .then(function (recordset) { //console.log(recordset.recordset); conn.close(); //NEED CALLBACK FUNCTION console.log(recordset.recordset.length); for(var i = 0; i<recordset.recordset.length; i++ ){ res.push(recordset.recordset[i]); } callback(null,recordset.recordset); process.exit(1); }) // Handle sql statement execution errors .catch(function (err) { console.log(err); conn.close(); }) }) // Handle connection errors .catch(function (err) { console.log(err); conn.close(); }); } //call Fn for db query with callback getProducts(function(err,data){ if (err) { // error handling code goes here console.log("ERROR : ",err); } else { // code to execute on data retrieval //console.log("result from db is : ",data.recordset); //return data.recordset; return res; } }); console.log(res); //WHY IS THIS EMPTY HERE? module.exports = { getProducts(){}, }; 

Blockquote

file2.js

 var myDB2 = require('./sqltest2'); console.log(myDB2.getProducts()); 

Here's my output in cmd: 在此处输入图片说明

After the '7', there's nothing showing.

My IDEAL output should be the following if I manage to get the returned data in file2.js from file1.js: 在此处输入图片说明

  1. You cannot see the res because its race condition, your console.log(res) executed earlier than recordset callback.

  2. file1.js already executed the getProducts function so there is not data that returned to file2.js .

 var sql = require("mssql"); // Create a configuration object for our Azure SQL connection parameters var dbConfig = { server: "***", // Use your SQL server name database: "***", // Database to connect to user: "***", // Use your username password: "***", // Use your password port: 1433, // Since we're on Windows Azure, we need to set the following options options: { encrypt: true } }; var obj = {}; var res = []; // This function connects to a SQL server, executes a SELECT statement, // and displays the results in the console. function getProducts(callback) { // Create connection instance var conn = new sql.ConnectionPool(dbConfig); conn.connect() // Successfull connection .then(function() { // Create request instance, passing in connection instance var req = new sql.Request(conn); // Call mssql's query method passing in params req.query("SELECT sp.product_name, count(ss.product_id) as 'quantity' " + "FROM smartcoolerstocks ss JOIN smartcoolerproducts sp " + "ON sp.product_id = ss.product_id " + "GROUP by sp.product_name ") .then(function(recordset) { conn.close(); callback(null, recordset.recordset); process.exit(1); }) // Handle sql statement execution errors .catch(function(err) { console.log(err); conn.close(); callback(err, null); }) }).catch(function(err) { console.log(err); conn.close(); callback(err, null); }); } module.exports = getProducts; 

And file2.js

 var myDB2 = require('./sqltest2'); myDB2(function(err, data){ if( err ) console.log(err); else console.log(data); }); 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM