0

I am trying to display database value in a variable but does not return any value.

below is the code.

var mssql = require('mssql');
var resultData='';
function getStyleData(){    
  var msSqlSettings = {
     server      : 'localhost',
     port        : '51663',
     user        : 'sa',
     password    : 'admin1234',
     database    : 'ComWriter.Temp002'
};  
mssql.close();  
mssql.connect(msSqlSettings, function (err ) { 
    if (err) {
        throw (err);
    }   
    var getQuery = "SELECT [Title] FROM [dbo].[Templates] " ;   
    var request = new mssql.Request();          
    request.query(getQuery, function (err, rows) {          
        //console.log(rows);
        mssql.close();
        resultData = rows;
    });
}); 
   return resultData;
}




var getFunction = getStyleData();

   console.log(getFunction);
RIYAJ KHAN
  • 15,032
  • 5
  • 31
  • 53
Sushil Kumar
  • 15
  • 1
  • 5
  • 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) – JJJ Jul 01 '17 at 14:27
  • can you log msSqlSettings and tell me what you get? – Mμ. Jul 01 '17 at 14:50

2 Answers2

0

It looks that you return resultData right after you call connect, maybe before the connection even established, and before query returned the result. Node is asynchronous so it doesn't wait for the results, try to console.log the rows inside the request.query call.

mssql.connect(msSqlSettings, function (err ) { 
    if (err) {
        throw (err);
    }   
    var getQuery = "SELECT [Title] FROM [dbo].[Templates] " ;   
    var request = new mssql.Request();          
    request.query(getQuery, function (err, rows) {          
        //console.log(rows);
        mssql.close();
        console.log(JSON.stringify(rows));
    });
}); 

You can pass in a callback to your getStyleData() and pass this callback to request.query() this way the rows will be returned asyncrounsly when the query will complete. This way you can pass in callback that wants to use the result.

getStyleDate(done){
    // your code ...
    mssql.connect(msSqlSettings, function (err ) { 
        if (err) {
            throw (err);
        }   
        var getQuery = "SELECT [Title] FROM [dbo].[Templates] " ;   
        var request = new mssql.Request();          
        request.query(getQuery, done);
    }); 

}
Lena Kaplan
  • 756
  • 4
  • 13
0

You can not return data from callback(asynchronous) in synchronous way

Need to refactor the code.

function getStyleData(callback) {
    var msSqlSettings = ..//;
    mssql.connect(msSqlSettings, function (err) {
        if (err) {
            callback("error message", null);
        }
        //query
        request.query(getQuery, function (err, rows) {

            resultData = rows;
            if (err) {
                callback("error message", null);
            } else {
                callback(null, resultDat);
            }
        });
    });
}



getStyleData(function (err, data) {

    if (err) {
        console.log("error");
    }

    console.log(data);
});
RIYAJ KHAN
  • 15,032
  • 5
  • 31
  • 53