1

I have a question, how can I output all rows from postgresql. Now I have some errors. Please help me. Thanks.

This is my code:

'use strict'

const res = client.query("SELECT * FROM public", function(err, rows, fileds) {
const row = [];
for(let i=0; i<rows.length; i++) {
    row = rows[i];
    console.log(row);
}
rows.forEach(async function(row) {
    console.log(row.name);
})
console.log('Finish');
});
const func = ms => new Promise(res => setTimeout(res, ms));
console.dir({func});
console.dir(res);
client.end();
2
  • What is the error ? Commented Mar 24, 2018 at 15:44
  • @parladneupane now I changed it. And it shows me { func: [Function: func] } and undefined Commented Mar 24, 2018 at 15:58

1 Answer 1

1

As you are not using pool yet, I assume you are using pg older than 6. You should do:

return client.query(sqlStatement)
    .then(res =>
    {
        client.end();

        return res.rows;
    })
    .catch(e =>
    {
        client.end();

        console.error(e);
        throw e;
    });
Sign up to request clarification or add additional context in comments.

5 Comments

What are you using to connect to postgres and what version?
Look in PostgreSQL I created database banking and there I have schemas where I have a table account. My version of pg is 7.4.1. So how can I output this table?
And I don't know why console shows me an error with query.on
Lado I have just changed the code using pg@7 format.
Cool. Thank you

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.