I am trying to extract database data in JavaScript using SQL Server. The problem now is that, after the connection close it seems that the data is not storing in the variable? I am not sure how it works.
const pool = new sql.ConnectionPool(config)
const poolConnect = pool.connect()
let workid = []
function connectStart(){
poolConnect.then(() => {
const request = new sql.Request(pool)
// 查询用户表格中的所有数据
request.query("select * from dbo.xxAIO_det where xxAIO__ch01 like '%172.168.1.70;%' order by xxAIO_NO", (err, result) => {
if (err) {
console.error("ERROR", err)
} else {
console.log("Results:")
result.recordset.forEach((row) => {
workid.push(row.xxAIO_NO);
})
}
console.log(workid)
pool.close()
})
})
}
connectStart()
console.log(workid)
the code seems to execute the final console log line and then start the connection, what is the problem or is this just how it behave?
[]
Results:
[ '10', '15', '20', '50' ]
I am not sure if this is the right way to get data from SQL Server, as I found this method online. The final goal is to extract the data and I need to display it in a HTML table.