1

I'm having trouble trying to get the Postgres data using Pg-Promise using this previous Stackoverflow question: Get the result of postgres-query as variable in nodejs.

If I do this code, the webpage just keeps loading and hangs, finally spitting out an EJS error "row_result is not defined". I just want to store the SELECT query results in a variable so I can use it outside of the db.task block. Any suggestions?

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();
});

In addition, is there any way to do this?

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});
mysticarcher
  • 105
  • 1
  • 1
  • 6
  • 1) `JSON.stringify(a)` - what is `a`? 2) `row_result:row_result` - that's not even valid JavaScript. 3) Where is your HTTP handler? 4) You need to narrow your question, it is all over the place. 5) Looks like your issue with promises more more than with `pg-promise`. You need to revise your question quite a bit. – vitaly-t Oct 10 '17 at 08:31
  • Not sure what you're talking about. row_result:row_result is a standard way to pass variables to EJS templates from Express. "a" is the variable that the task is returning, which is passing it to the "then" promise. – mysticarcher Oct 10 '17 at 16:09
  • 1
    "*is there any way to do this?*" - [**No**](https://stackoverflow.com/q/23667086/1048572) – Bergi Oct 10 '17 at 16:12

0 Answers0