I am trying to connect to a PostgreSQL database and make some queries. Though I have read a few threads here, the docs, and other blogs, I'm unable to make it works.
My function looks like this:
const postgreSQL = require('pg');
const config = {
host: 'host',
database: 'db',
user: 'user',
password: 'pass',
port: 0000,
max: 5, // max number of clients in the pool
idleTimeoutMillis: 30000
};
let pool = new postgreSQL.Pool(config);
//.........other code.........
function CheckGeometryType(pool, tableName, column) {
const schema = 'schema';
let geometryValue = undefined;
pool.connect(function(err, client, done) {
client.query(`SELECT ST_GeometryType(${column})
AS geometryType FROM ${schema}.${tableName} LIMIT 1`);
done(err);
if (err) {
console.log('Something went wrong. ' + err);
}
console.log("Result: " + queryResult.rows[0]);
switch (queryResult) {
// do things with result
}
done();
});
return geometryValue;
}
I've tried to do a console.log() inside the SWITCH, after/before done(), but it's not executed. I have printed all SQL queries formed on the string I pass to pool.query on pgAdmin and all works and return the expected values I want to get.
What am I doing wrong?
Thanks!
--- Edit ---
Thanks for the answers, but I'm still being unable to make it work. I've tried both methods but it isn't works. Looking at the Documentation I'm trying this method.
The code is:
let client = new Client(connectionString);
client.connect();
const query = client.query(new Query('SELECT ST_GeometryType($1) FROM $2.$3 LIMIT 1', [column, schema, tableName]));
console.log(JSON.stringify(query, null, 2)); //Debug purposes
query.on('row', (row) => {
switch (row[0]) {
case 'ST_LineString':
geometryValue = 'LineString';
break;
case 'ST_MultiLineString':
geometryValue = 'MultiLineString';
break;
case 'ST_Polygon':
geometryValue = 'Polygon';
break;
case 'ST_MultiPolygon':
geometryValue = 'MultiPolygon';
break;
case 'ST_Point':
geometryValue = 'Point';
break;
case 'ST_MultiPoint':
geometryValue = 'MultiPoint';
break;
default:
break;
}
});
query.on('end', (res) => {
if(result.rows[0] === undefined){
console.log('No data retrieved from DB');
}
client.end();
});
query.on('error', (res) => {
console.log("Something went wrong: " + res);
});
Debugging, I can see that on query the values are stored fine:
{
"text": "SELECT ST_GeometryType($1) FROM $2.$3 LIMIT 1",
"values": [
"column",
"schema",
"table"
],
"portal": "",
"_result": {
"command": null,
"rowCount": null,
"oid": null,
"rows": [],
"fields": [],
"_parsers": [],
"RowCtor": null,
"rowAsArray": false
},
"_results": {
"command": null,
"rowCount": null,
"oid": null,
"rows": [],
"fields": [],
"_parsers": [],
"RowCtor": null,
"rowAsArray": false
},
"isPreparedStatement": false,
"_canceledDueToError": false,
"_promise": null,
"domain": null,
"_events": {},
"_eventsCount": 0
}
I've executed the query on pgAdmin an works fine, returning the desired result that I'm expecting but seems that trying to execute the query on the script doesn`t works.
What I'm missing? Thanks.