Search code examples
node.jsherokuheroku-postgres

App Crashes When NodeJS is Asked to Talk to PostgreSQL


I have a modicum experience with web development, but the sites I've built to date have all had embedded databases. This is my first attempt at building a website with a non-embedded database.

I've built a skeleton site using NodeJS and the Express framework. It's hosted by Heroku. Before I tried to add in the database, the app seemed to work well, as expected.

However, now that I've added a scraper.js file to the app, all I get is a notification that the app has crashed. The contents of this file are:

const Finaliser = require("./finaliser.js");
const { Client } = require("pg");
const client = new Client({
  connectionString: process.env.DATABASE_URL,
  ssl: true,
});

class Scraper
{
  constructor()
  {
    this.finaliser = new Finaliser();
  }

  fetchAsIs(req, res)
  {
    var data, columns, rows;
    var tableName = req.params.id;
    var that = this;
    var queryString = "SELECT * FROM "+tableName+";";

    client.connect();
    client.query(queryString, (err, extract) => {
      if(err) throw err;

      client.end();

      console.log(extract);
      that.finaliser.protoRender(req, res, "asis",
                                 { title: tableName });
    });
  }
};

module.exports = Scraper;

Extra details:

  • Running heroku logs --tail gives:
2019-10-23T15:35:50.281517+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/" host=harvesthub.herokuapp.com request_id=caadba91-9b2d-4525-9c9c-19c34733073d fwd="194.33.13.237" dyno= connect= service= status=503 bytes= protocol=https
2019-10-23T15:35:50.588155+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/favicon.ico" host=harvesthub.herokuapp.com request_id=28b7948f-ff59-44f0-867a-f3ce7ede86cc fwd="194.33.13.237" dyno= connect= service= status=503 bytes= protocol=https
  • I think the database is set up correctly. Running heroku addons --app harvesthub gives:
Add-on                                            Plan       Price  State  
────────────────────────────────────────────────  ─────────  ─────  ───────
heroku-postgresql (postgresql-crystalline-06305)  hobby-dev  free   created
 └─ as DATABASE

The table above shows add-ons and the attachments to the current app (harvesthub) or other apps.
  • The repository for the app's source code is https://github.com/tomhosker/harvesthub.

  • I know that "SELECT * FROM "+tableName+";" is a recipe for sorrow. I'll change it later.

  • Sorry for such a long question!


Solution

  • The problem was that I had failed to install the pg and dotenv packages properly. (I had attempted to install them, but for some reason it didn't take.)

    To anyone finding himself in the same position as me: double check that pg and dotenv are in your package.json file. If they're missing, run npm install --save pg dotenv to install them.

    Heartfelt thanks to Justin Moser. This answer is just a fleshing out of his comment.