So I have information coming in the GET URL, which needs to get passed into JSON and then saved (aggregated with increasing IDs to be correct) in the PostgreSQL DBMS. I wrote the following code, which seems to not save anything with no errors:
// Pg initialization
const { Client } = require('pg')
client = new Client({
host: 'localhost',
user: 'postgres',
password: 'passwordhere',
database: 'dbnamehere',
});
const createTableText = `
CREATE EXTENSION IF NOT EXISTS "pgcrypto";
CREATE TEMP TABLE IF NOT EXISTS cases (
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
data JSONB
);
`
app.get('/test', async function (req, res) {
data = req.query.data;
console.log(data);
res.status(200).send(data);
// create our temp table
await client.query(createTableText)
//const newUser = { email: '[email protected]' }
// create a new case
await client.query('INSERT INTO cases(data) VALUES($1)', [data])
const { rows } = await client.query('SELECT * FROM cases')
console.log(rows)
res.end();
});
My package.json dependencies:
"dependencies": {
"express": "^4.17.1",
"mongoose": "^5.9.9",
"pg": "^8.0.3"
},
"devDependencies": {}
UPDATE
I have this error handling code at the end of the file:
// Prints out more detailed errors
if(process.env.NODE_ENV !== 'production') {
process.once('uncaughtException', function(err) {
console.error('FATAL: Uncaught exception.');
console.error(err.stack||err);
setTimeout(function(){
process.exit(1);
}, 100);
});
}
I also tried installing npm install express-promise-router and adding the following code but no errors were printed:
var router = require('express-promise-router')();
router.use('/test', function (req, res) {
return Promise.reject();
})
UPDATE2 This code without closing it prints out the JSONB, not how do I save it?:
const connectionString=urlhere;
const pool = new Pool({
connectionString: connectionString,
})
const client = new Client({
connectionString: connectionString,
})
client.connect()
UPDATE3:
I removed the asynchronous code and made it synchronous. I get the following error messages now:
(node:10860) UnhandledPromiseRejectionWarning: Error: Connection terminated
at Connection.<anonymous> (/path/here/node_mo
dules/pg/lib/client.js:275:34)
at Object.onceWrapper (events.js:299:28)
at Connection.emit (events.js:215:7)
at Socket.<anonymous> (/path/here/node_module
s/pg/lib/connection.js:73:10)
at Socket.emit (events.js:210:5)
at TCP.<anonymous> (net.js:659:12)
(node:10860) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This
error originated either by throwing inside of an async function without a catch
block, or by rejecting a promise which was not handled with .catch(). (rejectio
n id: 1)
(node:10860) [DEP0018] DeprecationWarning: Unhandled promise rejections are depr
ecated. In the future, promise rejections that are not handled will terminate th
e Node.js process with a non-zero exit code.
(node:10860) UnhandledPromiseRejectionWarning: Error: Connection terminated
at Connection.<anonymous> (/path/here/client.js:275:34)
at Object.onceWrapper (events.js:299:28)
at Connection.emit (events.js:215:7)
at Socket.<anonymous> (/path/here/node_module
s/pg/lib/connection.js:73:10)
at Socket.emit (events.js:210:5)
at TCP.<anonymous> (net.js:659:12)
(node:10860) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This
error originated either by throwing inside of an async function without a catch
block, or by rejecting a promise which was not handled with .catch(). (rejectio
n id: 2)
(node:10860) UnhandledPromiseRejectionWarning: Error: Connection terminated
at Connection.<anonymous> (/path/here/node_mo
dules/pg/lib/client.js:275:34)
at Object.onceWrapper (events.js:299:28)
at Connection.emit (events.js:215:7)
at Socket.<anonymous> (/path/here/node_module
s/pg/lib/connection.js:73:10)
at Socket.emit (events.js:210:5)
at TCP.<anonymous> (net.js:659:12)
(node:10860) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This
error originated either by throwing inside of an async function without a catch
block, or by rejecting a promise which was not handled with .catch(). (rejectio
n id: 3)
npm install express-promise-router? I added my package.json dependencies for clarity.