0

During the launch my website i can't connection to my database. It doesn't get errors and the page is loading infinitely. Who know answer on this question give me some advise.

I work in (Node.js,PostgreSQL,Express)

from file config_db

const client_admin1 = {
    user:'a_role',
    database:'road_db',
    password: '12345',
    host:'localhost',
    port:5432,
    max:10,
    idleTimeoutMillis: 30000
}
const pg = require("pg")

const client_login = require("../config/config_db");


     searchUser(req, res) {
            const db = new pg.Pool(client_login.client_admin1)
            db.connect(function(err, client, done){
            if(err){
            return console.error('connection problems')
             }
             client.query('SELECT * FROM login_phone WHERE phone = $1 AND password1 = $2',[req.body.login, req.body.password], function(err, result){
                    done()
                    if (err || result.rowCount <= 0){
                       res.render('error_login_choice')
                        return console.error("")
                    }
                    if(result.rows[0].role_name === "client")
                    {
                            cln.getClients(req,res)
                    }
                })
}

1 Answer 1

1

try this executeQuery method

const pg = require('pg');

const pgconfig = {
  user: process.env.DATA_BASE_USER,
  database: process.env.DATA_BASE_NAME,
  password: process.env.DATA_BASE_PASSWORD,
  host: process.env.DATA_BASE_HOST,
  port: process.env.DATA_BASE_PORT,
};

console.log(`DB |  Settings: ${JSON.stringify(pgconfig)}`);

const pool = new pg.Pool(pgconfig);

pool.on('connect', () => {
  console.log('DB | new client connection establish.');
});

pool.on('error', err => {
  console.error(`idle client error, ${err.message} | ${err.stack}`);
});

pool.connect(err => {
  if (err) {
    console.error(`PostgreSQL input: ${err}`);
  } else console.log('DB | connection establish.');
});

const executeQuery = async(sql, data) => {
  logger.debug(`sqlToDB() sql: ${sql} | data: ${data}`);
  try {
    const result = await pool.query(sql, data);
    return result;
  } catch (error) {
    console.error(error.message);
  }
};

module.exports = {
  executeQuery,
};

usage:

const dbHandler = require('./db');

    const getLogedInUser = login,password => {
      const selectFieldsQuery = `'SELECT * FROM login_phone WHERE phone = $1 AND password1 = $2'`;
      return dbHandler.executeQuery(selectFieldsQuery, [login,password]).then(e => e.rows);
    };
Sign up to request clarification or add additional context in comments.

2 Comments

Sorry, this is hard. I have 2 laptops and on the first one my code works completely, but on the second one it doesn't and i don't understand why
do you have Postgres installed properly on both of them?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.