我在使用此前Stackoverflow问题尝试使用Pg-Promise获取Postgres数据时遇到了麻烦:Get the result of postgres-query as variable in nodejs。
如果我执行此代码,网页将继续加载并挂起,最后吐出EJS错误“ row_result未定义”。我只想将SELECT查询结果存储在变量中,以便我可以在db.task块之外使用它。有什么建议吗?
var row_result = "";
db.task(function * (t) {
let a= yield t.query("SELECT * FROM public.applications;");
return {a};
})
.then(data => {
row_result = JSON.parse(JSON.stringify(a));
res.render('/applications', {row_result:row_result});
})
.catch(error => {
console.log('error');
})
.finally(() => {
pgp.end();
});
另外,有没有办法做到这一点?
var row_result = "";
db.task(function * (t) {
let a= yield t.query("SELECT * FROM public.applications;");
return {a};
})
.then(data => {
row_result = JSON.parse(JSON.stringify(a));
})
.catch(error => {
console.log('error');
})
.finally(() => {
pgp.end();
});
res.render('/applications', {row_result:row_result});