3

So I am not sure how node-pg works but I have been trying to make it work by looking around, trying things and reading the documentation for a while now. What I want to achieve is to be able to send multiple queries at different times but it always throws errors after i send the first one if I dare coding a second one in. This question has been asked multiple times online but I dont see any helpful answers. I can use pooling if its necessary (that had issues too).

What works now: (Its on heroku so i have to use process.env.DATABASE_URL)

const { Client } = require('pg');

const client = new Client({
connectionString: process.env.DATABASE_URL,
ssl: {
rejectUnauthorized: false
  }
});
client.connect()

client

.query('SELECT * FROM test WHERE....')
.then(results => {
console.log(results);
}

.catch(err => {
console.log(err.stack)
 })

What I 'm trying to do:

const { Client } = require('pg');

const client = new Client({
connectionString: process.env.DATABASE_URL,
ssl: {
rejectUnauthorized: false
  }
});
client.connect()

client

.query('SELECT * FROM test1 WHERE....')
.then(results => {
console.log("FIRST RESULTS: "results);
}

.catch(err => {
console.log(err.stack)
 })

.query('SELECT * FROM test2 WHERE....')
.then(results => {
console.log("SECOND RESULTS: "results);
}

.catch(err => {
console.log(err.stack)
 })
1
  • I tried multiple things so i got multiple different errors. I was probably not doing it right but I cant find any example online with more than one query thats why i came here. While using pooling, when i used specific clients like this example: node-postgres.com/features/pooling the error was ''client.query(...).then(...).query is not a function'' on the second query, while without clients its similarly: "TypeError: pool.query(...).then(...).catch(...).query is not a function" Commented Sep 21, 2020 at 22:57

1 Answer 1

2

When you .query, a Promise is returned, and Promises don't have .query methods - all you can do with the Promise is call .then or .catch on it (or .finally, sometimes).

If you want to make a new query after the old finishes, call client.query inside a .then:

client
  .query('SELECT * FROM test1 WHERE....')
  .then(results => {
      console.log("FIRST RESULTS: " + results);
    })
    .then(() => client.query('SELECT * FROM test2 WHERE....'))
    .then(results => {
      console.log("SECOND RESULTS: " + results);
    })
    .catch(err => {
      // better to handle errors at the end, probably
      console.log(err.stack)
    })

If the second query depends on the first's result, consider using an async function instead, to make the code flatter and more readable.

If the second query doesn't depend on the first's result, consider using Promise.all instead, so that both queries can be made at once, rather than having to wait for the first to finish first.

Sign up to request clarification or add additional context in comments.

4 Comments

Thank you very much. Finally, that works. That answers my question so I'm accepting it but Im not sure what i should do if i need more queries later in a different block of code. Can I just call a function that includes the same process again? "Client.query(..).then(..)" because when i tried that i got yet another error. It works fine in consecutive .THENs but I do not know what to do in that other instance. If you are willing to help more I would appreciate it. Thank you.
Any time you have more queries to run, return them inside .thens so that the next .then can deal with their resolve values. If you want to run two queries at once, then do return Promise.all([client.query(...), client.query(..)]) and .then(([result1, result2]) => { /* do stuff with result1 and result2 */
Thank you, i think i figured it all out with your help. Hopefully last question, when should i use client.end()?? If i understand it right once i use it then i can not send any more queries again not even reopen it somehow right?
I think you'd call it once you're finished, like after a finally. .catch(...).finally(() => { client.end(); });

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.