let RetrieveFromDb=(queryStr) =>
{
let sql=require('mssql');
let config={
server:'127.0.0.1',
database:'TestDB',
user:'user',
password:'user',
};
let dbConn=new sql.Connection(config);
return dbConn.connect().then(function (){
let request=new sql.Request(dbConn);
return request.query(queryStr)
.then(function(recordset)
{
return recordset;
dbConn.close();
}).catch(function (err) {
console.log(err);
dbConn.close();
});
}).catch(function (err) {
console.log(err);
});
};
RetrieveFromDb("Select FirstName from User where UserID='1'").then(function(x){console.log(x[0].FirstName);});//console is displying the FirstName value from DB
RetrieveFromDb("Select FirstName from User where UserID='1'").then(res=>{console.log(res[0].FirstName)});//console is displying the FirstName value from DB
以上两个函数调用将值返回到控制台,但我想将 RetrieveFromDb() 的结果保存到一个变量中,以便我可以与其他数据进行比较以进行验证。
假设如果 varactualFirstname=RetrieveFromDb(myquery);
那么我会比较if(actualFirstname===expectedFirstName)
与否。