Error when using pool connections in Express Framework using node.js

Viewed 62

my DB.js:

const dbConfig = require("./DBConfig")
const pg = require('pg');
const connectionString = dbConfig.URL;

const pool = new pg.Pool({
    connectionString,
})

module.exports = pool;

DBConfig.js:

const USERNAME = "postgres";
const DATABASE = "elearning_site";
const PASSWORD = "root";
const PORT = "5432";
const HOST = "localhost";

const URL = 'postgresql://${USERNAME}:${PASSWORD}@${HOST}:${PORT}/${DATABASE}';

module.exports = { URL }

my service file where im using the pool connection :

const pool = require("../db/DB.js")

class CourseService{
    saveCourse(error, success){
        const sql = "insert into courses(name,start_date,status,paid_status,department_id,credit_hours)values('JAva','2016-06-22 19:10:25-07','ongoing','free','28','4')";
            
                pool.query(sql, (err,res)=>{
                    if(err){
                    error("Cannot connect to DB");

                    }else{
                        console.log(res);
                        success(res);
                    }
                })
            
       

    }
}

module.exports = new CourseService()

the following code gives me the error in the terminal, when called from my controller :

Error: read ECONNRESET
    at TCP.onStreamRead (internal/stream_base_commons.js:209:20) {
  errno: -4077,
  code: 'ECONNRESET',
  syscall: 'read'
}
Error: read ECONNRESET
    at TCP.onStreamRead (internal/stream_base_commons.js:209:20) {
  errno: -4077,
  code: 'ECONNRESET',
  syscall: 'read'
}

where as an "Cannot connect to DB" is seen on the page.

even when my database is up and running.

my controller :

const courseService = require("../services/CourseService")


class CourseController{
        saveCourse(req, res){
        courseService.saveCourse((err)=>{
            res.json(err)
        }, (message)=>{
            res.send( {message: message})
            
        })
      
    }
  
}

module.exports = new CourseController()

saveCourse is the controller function being used in the router.

0 Answers
Related