我想运行两个查询,当它们完成时将数据传递给 res.render
。我读过有关 promise 的内容,但我仍然不明白如何使第二个查询传递数据并将其放入渲染中。基本上我想运行 queryTable
函数/promise 两次,但第二次使用 thegraphquery
。
app.get('/clickus', function(req, res) {
var thequery = 'SELECT ab FROM table';
var thegraphquery = `select cd FROM table `;
let promise = queryTable(thequery);
promise.then(
data => res.render('clickus', {
'mydata': data
})
);
});
function queryTable(thequery) {
return new Promise(function(resolve, reject) {
var con = new msSqlConnecter.msSqlConnecter(config);
con.connect().then(function() {
new con.Request(thequery).onComplate(function(count, datas) {
resolve(datas);
}).onError(function(err) {
console.log(err);
}).Run();
}).catch(function(ex) {
console.log(ex);
});
});
}
最佳答案
使用Promise.all
同时执行两个查询并等待两个结果,然后再将它们传递给res.render
:
app.get('/clickus', function(req, res) {
var thequery = 'SELECT ab FROM table';
var thegraphquery = `select cd FROM table `;
Promise.all([queryTable(thequery), queryTable(thegraphquery)])
.then(results => {
res.render('clickus', {
'queryData': results[0],
'graphQueryData': results[1]
});
});
});
关于javascript - 如何在 Node 中运行两个查询并将它们传递给渲染函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52911000/