我试图使用节点执行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();
})
请注意,customers表是我的电子商务数据库中的一个有效表。
任何帮助都将不胜感激。
发布于 2022-11-16 16:50:59
您应该等待客户端连接。
见docs 这里
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')
,而是编写查询。
https://stackoverflow.com/questions/74464215
复制相似问题