0

I have Node.js express app with Postgres as a database. I'm using pg for database communication from the app.

This is how my db.service looks like

import { Pool } from 'pg';

const dbConfig = {/*my valid db configuration*/};

const pool = new Pool(dbConfig);

export const connectDB = async () => {
 let client;
 try {
  client = await pool.connect();
 } catch (error) {
  console.error('error while connecting to database', error);
  process.exit(1);
 }
 return client;
};

I have two queries as below

#1.

export const fetchUser = async (email) => {
const client = await connectDB();

  const query = `
    SELECT full_name FROM app.users
    WHERE  email = '${email}'
  `;

  let result;
  try {
    result = await client.query(query);
    if (result.rowCount) {
      return result.rows[0].full_name;
    }
  } catch (error) {
  } finally {
    await client.release();
  }
  return result;
};

#2

export const fetchWallet = async (email) => {
    const client = await connectDB();
    
      const query = `
        SELECT wallet_money FROM app.user_wallet
        WHERE  email = '${email}'
      `;
    
      let result;
      try {
        result = await client.query(query);
        if (result.rowCount) {
          return result.rows[0].wallet_money;
        }
      } catch (error) {
      } finally {
        await client.release();
      }
      return result;
    };

Now from one of my controller.js if I call these function separate await, no issues

ctrl.js

   const fullName = await fetchUser('[email protected]');
   const walletMoney = await fetchWallet('[email protected]');

No issues this way, however if I merge them into a single promise

   const $0= fetchUser('[email protected]');
   const $1= fetchWallet('[email protected]');
  
   const result = await Promise.all([$0, $1]);

this throws the below error

error while connecting to database Error: timeout exceeded when trying to connect at Error

Please suggest why this error is popping up & how can I get rid of it?

Thanks!

3
  • This is related to your db connection file. Please add logs and check if the config is proper and also your db server is running correctly Commented Jan 25, 2023 at 12:09
  • @DeekshithHegde I don't think so because if that would have been the case in the approach #1 where I have separate await it executes with no issues Commented Jan 25, 2023 at 12:12
  • Related: Node-Postgres Error: timeout exceeded when trying to connect Commented Aug 21 at 18:18

1 Answer 1

0

That's happening because you are trying to connect to DB separately for every query.Try to create one connection and use it for all queries!

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

Comments

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.