【问题标题】:Error when trying to perform postgres queries (triggerUncaughtException(err, true /* fromPromise */);尝试执行 postgres 查询时出错 (triggerUncaughtException(err, true /* fromPromise */);
【发布时间】:2022-11-17 00:50:59
【问题描述】:

我试图使用节点执行 postgres 查询,但由于某种原因没有从 postgres 获取数据。

请参阅下面的 database.js:

const { Client } = require('pg');
const PORT = process.env.PORT || 3000;

const client = new Client({
    host: 'localhost',
    port: PORT,
    user: 'postgres',
    password: 'postgres',
    database: 'ecommerce'
})

client.connect();

client.query('select * from customers', (err, result) => {
    if (!err) {
        console.log(result.rows)
    } else {
        console.log(err)
    }
    client.end();
})

请注意,客户表是我的电子商务数据库中的有效表。

任何帮助,将不胜感激。

【问题讨论】:

  • 请阅读How to Ask,你能发布完整的错误吗?

标签: node.js postgresql


【解决方案1】:

您应该等待客户端连接。

请参阅文档here

const { Client } = require('pg')
const client = new Client()
client
  .connect()
  .then(() => console.log('connected'))
  .catch(err => console.error('connection error', err.stack))

而不是console.log('connected'),写下你的查询。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-05-25
    • 1970-01-01
    • 1970-01-01
    • 2020-11-06
    • 2016-01-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多