我正在尝试查询 mssql 数据库以获取用户的密码,即发送 post 请求,然后将其显示在控制台中。但我做出的承诺并没有按照我想要的方式工作,并且密码没有加载到变量中。这是我的代码:
app.post('/test', function () {
let user = 'User';
let query = 'SELECT [Password] as password FROM [Table] where [User] = ' + SqlString.escape(user);
let password = (async function () {
try {
let pool = await sql.connect(dbConfig);
let result = await pool.request()
.query(querys);
console.dir(result.recordset[0].password) //Value here is OK
return result.recordset[0].password
} catch (err) {
// ... error checks
}
})()
console.log(password); //here I am getting "Promise { pending }"
});
我得到的结果是:Promise {pending}
'数据库密码'
这是一个使用的例子async / await
与快递。
Express 不处理错误,但这不是什么大问题,您可以自己处理它们,您甚至可以包装到通用错误处理程序中以使事情变得更容易。
无论如何,这是您修改后使用的代码async/await
在快递中。
app.post('/test', async function (req, res) {
try {
const user = 'User';
const query = 'SELECT [Password] as password FROM [Table] where [User] = ' + SqlString.escape(user);
const pool = await sql.connect(dbConfig);
const result = await pool.request()
.query(querys);
const password = result.recordset[0].password;
console.log(password);
res.end(password);
} catch (e) {
res.end(e.message || e.toString());
}
});
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)