我正在尝试连接到PostgreSQL数据库并进行一些查询。尽管我在这里阅读了一些主题,文档和其他博客,但我无法使其正常运行。

我的函数如下所示:



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;
}





我已尝试在done()之后/之前在SWITCH内执行console.log(),但未执行。我已经打印了在传递给pgAdmin上的pool.query的字符串上形成的所有SQL查询,并且所有工作都返回了我想要获得的期望值。

我究竟做错了什么?

谢谢!

-编辑-

感谢您的回答,但我仍然无法使其正常工作。我已经尝试了两种方法,但是都行不通。看着Documentation我正在尝试这种方法。

代码是:



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);
});





调试时,我可以看到在查询时值存储得很好:

{
  "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
}


我已经在pgAdmin上执行了查询,效果很好,返回了我期望的期望结果,但是似乎无法在脚本上执行查询。

我想念的是什么?谢谢。

最佳答案

您还可以将原始acces与“ pg”包一起使用:

pg.connect(connectionString, function (err, client, done) {
  if (err) {
    return handleErrors(done, err, res);
  }
  return client.query("UPDATE tab SET v1=($1)  WHERE k1=($2)", [value, key],
    function (err, result) {
     return reloadData(res, client, done, err);
  });
});

09-25 10:48