0
  • New to Node here, trying for the last 3 days straight, no clue.
  • Read all similar issues and tried literally everything I could find, no luck.
  • Suspecting something that is not common or related to my machine or code.

  • Issue: trying to fetch data in node.js from postgres db - to console (at least) so to render it later as HTML

  • Database has table name: students on heroku, and has records

  • Also locally on my macOS, I have postgres installed with simple data in a table called students

  • I couldn't fetch any data, no error, no idea how to track it!

  • Tried creating connection with pool, client.. also used heroku guide here exactly

  • Literally everything that other users mostly encountered

  • DATABASE_URL environment variable is ok, if i echo $DATABASE_URL in Terminal:


postgres://xnlkikdztxosk:kuadf76d555dfab0a6c159b8404e2ac254f581639c09079baae4752a7b64a@ec3-52-120-48-116.compute-1.amazonaws.com:5432/uytnmb7fvbg1764

  • When i run 'node app.js' server starts ok on port 3000, I can use postman on the root '/' OK and it works, it returns back the json info and console.log
  • If i try postman to '/students' then it tries forever, no results, no error, nothing
  • Tried also with my local installation of postgres, same thing
  • My modules are ok, and I run npm install several times
  • Thought could be my mac firewall, i turned it off completely
  • Also tried this, nothing prints out or no idea where to track it:

process.on('uncaughtException', function (err) {
  console.log(err);
}); 

  • Guide or steps to follow in order to track issues like this will be highly appreciated

    app.js file:
    
      const express = require('express')
      const bodyParser = require('body-parser')
      const { Client } = require('pg');
    
      const app = express()
      const PORT = 3000
    
      app.use(bodyParser.json())
      app.use(
        bodyParser.urlencoded({
          extended: true,
        })
      )
    
      const client = new Client({
        connectionString: process.env.DATABASE_URL,
        ssl: {
          rejectUnauthorized: false
        }
      });
      client.connect();
    
      app.get('/', (req, res) => {
        res.json({ info: 'Info: This is the root directory' });
        console.log('main directory')
      })
    
      app.get('/students', (req, res) => {
    
        client.query('SELECT * FROM students;', (err, res) => {
          if (err) throw err;
          for (let row of res.rows) {
            console.log(JSON.stringify(row));
            console.log('WHOOOOOO, finally!');
          }
          client.end();
        });
      });
    
      app.listen(PORT, function(){ 
        console.log('Server running on port 3000');
      });
    

2 Answers 2

1

Well, my node version was for some reason v14, not sure how that happened, but the most stable version in node site is 12, so I installed v12 and the connection to pg worked locally and remotely on heroku.

  • This is just to highlight what worked with me after trying 4 days straight.

However, that may trigger for you different issue like like this which I'm facing:

DeprecationWarning: Implicit disabling of certificate verification is deprecated and will be removed in pg 8. Specify `rejectUnauthorized: true` to require a valid CA or `rejectUnauthorized: false` to explicitly opt out of MITM protection.
  • All answers found so far point to: pg module already fixed in v7.18.1 but for some reason I can't force package.json to take that version, it jumps me to version 7.18.2
  • Tried that along with latest version 8.3 same issue with heroku, but locally the message doesn't show

Not big deal though, connection works for now until figuring it out.

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

Comments

0

I think the issue here is that you don't send back any response in the /students route .Notice the / route u have a res.json which sends back a response but in /students route i don't see where your response is sent and that's why you wait forever

1 Comment

Response res is there in the code.. found out the issue, here it is below.

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.