D
D
denstuk2020-05-02 20:16:25
Node.js
denstuk, 2020-05-02 20:16:25

How to get object from pool.query query (Node Js, PostgreSQL)?

You need to get an object for further use.
First I tried to do this:

const getUsers = async () => {
    await pool.query('select * from users', (err, result) => {
        if (err) {
            console.log(err);
        }
        return result.rows;
    });
}
const data = getUsers();
console.log(data);

But at the output I got Promise { pending }.
Added .then() to fix the bug:
let data = [];
getUsers().then((result) => {
    data = result;
    console.log(data);
});

But the output is undefined.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
O
origami1024, 2020-05-02
@denstuk

const getUsers = async () => {
    let real_result = await pool.query('select * from users', (err, result) => {
        if (err) {
            console.log(err);
        }
        return result.rows;
    });
  return real_result;
}

let data = [];
getUsers().then((result) => {
    data = result;
    console.log(data);
});

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question