javascript - Close MSSQL connection using mssql in node.js -
i trying write script in node.js query mssql database. new javascript, new node.js, new vscode, know few things sql. have working code, connection never seems close, , cannot values out of function.
so, have chunk of code, got example npm:
const sql = require('mssql'); var dbconfig = { server:'theserver', database:'thedb', user:'un', password:'pw', port:1433 }; sql.connect(dbconfig).then(pool => { // query return pool.request() .query('select top 10 * the_table') }).then(result => { console.log(result); }).catch(err => { // ... error checks })
this works, , can see 10 results logged in console. however, code never stops running. how connection close , stop?
i want results saved variable, changed code this:
const sql = require('mssql'); var dbconfig = { server:'theserver', database:'thedb', user:'un', password:'pw', port:1433 }; let thelist; sql.connect(dbconfig).then(pool => { // query return pool.request() .query('select top 10 * the_table') }).then(result => { thelist= result; }).catch(err => { // ... error checks }) console.log(thelist);
this returns 'undefined' console thelist, , again connection never seems cose, , script never shuts down.
how grab results of query , move on down road??
you should call
process.exit()
in end. source node docs
moreover
sql.connect(dbconfig).then(pool => { // query return pool.request() .query('select top 10 * the_table') }).then(result => { thelist= result; }).catch(err => { // ... error checks })
is async function
console.log(thelist);
would not wait update value.
i think want do
sql.connect(dbconfig).then(pool => { // query return pool.request() .query('select top 10 * the_table') }).then(result => { thelist= result; }).catch(err => { // ... error checks }).then(function(){ console.log(thelist); process.exit(1); })
Comments
Post a Comment