Hopefully this is a minimum viable example ->
routes.ts
import express, { Request, Response } from "express";
import { QueryResult, Pool } from "pg";
const pool = new Pool({
user: process.env.DOCKER_USER,
host: "localhost",
database: process.env.DOCKER_DB,
password: process.env.DOCKER_PASSWORD,
port: 5432,
});
const router = express.Router();
router.post("/log/hub", (req: Request, res: Response) => {
console.log("Made it here!");
const username = "PLACEHOLDER";
const json = { a: "b" };
const now = new Date();
const cachedId = "12345";
pool.query(
"INSERT INTO data_actions (username, json_payload, cache_id, action_timestamp) VALUES ($1, $2, $3, $4)",
[username, json, cachedId, now],
(error: Error, results: QueryResult) => {
if (error) {
throw error;
}
res.status(201).send(results);
}
);
});
export = router;
echo $DOCKER_USER,$DOCKER_DB,$DOCKER_PASSWORD
produces
docker, docker, docker
I have verified in the client that these values match the values printed above and that they match the credentials set up in the Docker container for Postgres. I'm able to connect to the database on port 5432 with pgAdmin4 and view the database I expect, which looks like this:
List of relations
Schema | Name | Type | Owner
--------+------------------+-------+--------
public | data_actions | table | docker
(1 row)
SELECT * FROM data_actions;
produces
username | json_payload | cache_id | action_timestamp
----------+--------------+----------+------------------
(0 rows)
I am able to reach the express router endpoint at localhost:5000, so everything downstream is not the problem. I think the issues lies somewhere with how I'm using pg
. Does anything obvious stand out here? I have a feeling I'm missing something small and I'm banging my head against the keyboard trying to figure out what's going wrong.
EDIT: The relation shows 1 row where select statement returns 0 rows. This is because I deleted the row I inserted from pgAdmin before I posted here. Sorry for the red herring.
Try updating the pg
module. Old versions might not work with newer Nodejs versions.