Wednesday 26 December 2018

[HELP] Scope issue?

Here is a quick excerpt of what I'm doingvar express = require('express'); var app = express(); var sql = require('mssql/msnodesqlv8'); var config = { connectionString: 'Driver=SQL Server;Server=SQLServer;Database=DEDB;Trusted_Connection=true;' }; app.get('/this/:id/thing', function(req, res){ var id = req.params.id; var data; var dataQry = "SELECT * FROM qTable WHERE ID = '" + id + "'" sql.connect(config, function (err) { if (err) console.log(err); var request = new sql.Request(); request.query(dataQry, function (err, recordset) { if (err){ console.log(err); } else { data = recordset; } sql.close(); }); }); console.log(data); //This is where I would like the data to be avalible so I can make one more call to a DB then //send both sets of data to my template }); The final console.log(data) returns as undefined. I am rather new to JS so I'm not sure what I'm missing. If I console.log(data); right under my attempt to assign recordset to the higher scope data, it works just fine. Outside of the callback data is still undefined. Any help or pointing in the correct direction is greatly appreciated!

Submitted December 26, 2018 at 06:08PM by TuxMux080

No comments:

Post a Comment