I'm doing some basic CRUD operations on Postgres DB with Node app using 'pg' (node-postgres). Seems to have some issue making this particular query - the whole app hangs, like the Promise never resolves. The execution stops at this point and doesn't throw error.
The whole thing worked before I changed the DB structure a little bit and refactored the code (made it more modular, etc.). The database is up and connected (using Cloud9's Ubuntu 14.04 LTS container), other query checks successfuly whether it exists. This failing query ('select * from inventory ...') maually works fine - when I type it in the psql CLI, I get result of 0 rows, as it should be. So my best guess is, something in the async/await logic is just not right.
// This part executes without issues
const initDB = async () => {
try {
await client.connect();
console.log('Client connected to db ...');
const checkTable = await client.query("SELECT EXISTS (SELECT 1 FROM information_schema.tables WHERE table_name = 'inventory')");
if (!checkTable.rows[0].exists) {
const table = await client.query('CREATE TABLE inventory (prod_id serial PRIMARY KEY, product_name VARCHAR (50), category TEXT, description TEXT, price REAL, width INT, depth INT, height INT, checksum TEXT, history REAL ARRAY)');
}
} catch (err) {
console.log("Error initializing db.", err);
}
};
// This part fails
Promise.all([getProductData(productsUrlBase)]).then(async (values) => {
// checksum = ...
try {
console.log('This logs on the console');
const check = await client.query('SELECT * FROM inventory WHERE checksum=$1', [checksum]);
console.log(check.rows, 'if this is empty, INSERT in DB!'); // Never logs on the console. App doesn't throw error, either.
} catch (err) {
console.log('Query error - checking / inserting items: ', err);
}
});
Postgres log file:
2019-04-19 12:18:53 UTC LOG: database system was interrupted; last known up at 2019-04-19 08:39:56 UTC
2019-04-19 12:18:54 UTC LOG: database system was not properly shut down; automatic recovery in progress
2019-04-19 12:18:54 UTC LOG: record with zero length at 0/17A3BF8
2019-04-19 12:18:54 UTC LOG: redo is not required
2019-04-19 12:18:54 UTC LOG: MultiXact member wraparound protections are now enabled
2019-04-19 12:18:54 UTC LOG: database system is ready to accept connections
2019-04-19 12:18:54 UTC LOG: autovacuum launcher started
2019-04-19 12:18:54 UTC LOG: incomplete startup packet
I expected to get Error at least, but nothing happens. You can see the Postgres server logs - to me it doesn't seem to have any significant problems there.
Here's the full GitHub repo. The problematic part is in ./data-handlers/update.js
updateDB(). It is called in app.js like this for testing purposes:(async() => { await initDB(); updateDB(productsUrlBase, categories[0]); })();A query inside this IIFE works fine, like this:(async() => { await initDB(); let testQuery = client.query('SELECT * FROM inventory').then(rows => { console.log(rows); // This logs the DB info. }); updateDB(productsUrlBase, categories[0]); })();