【发布时间】:2020-09-22 07:00:40
【问题描述】:
所以我不确定 node-pg 是如何工作的,但我一直试图通过环顾四周、尝试一些事情并阅读文档一段时间来使其工作。我想要实现的是能够在不同的时间发送多个查询,但是如果我敢于编写第二个查询,那么在我发送第一个查询后它总是会抛出错误。这个问题已经在网上被问过多次,但我认为没有任何帮助答案。如果有必要,我可以使用池化(这也有问题)。
现在有效:(它在 heroku 上,所以我必须使用 process.env.DATABASE_URL)
const { Client } = require('pg');
const client = new Client({
connectionString: process.env.DATABASE_URL,
ssl: {
rejectUnauthorized: false
}
});
client.connect()
client
.query('SELECT * FROM test WHERE....')
.then(results => {
console.log(results);
}
.catch(err => {
console.log(err.stack)
})
我要做什么:
const { Client } = require('pg');
const client = new Client({
connectionString: process.env.DATABASE_URL,
ssl: {
rejectUnauthorized: false
}
});
client.connect()
client
.query('SELECT * FROM test1 WHERE....')
.then(results => {
console.log("FIRST RESULTS: "results);
}
.catch(err => {
console.log(err.stack)
})
.query('SELECT * FROM test2 WHERE....')
.then(results => {
console.log("SECOND RESULTS: "results);
}
.catch(err => {
console.log(err.stack)
})
【问题讨论】:
-
我尝试了很多东西,所以我得到了多个不同的错误。我可能做得不对,但我在网上找不到任何示例,其中有多个查询,这就是我来这里的原因。在使用池时,当我使用像这个例子这样的特定客户端时:node-postgres.com/features/pooling 在第二个查询中,错误是 ''client.query(...).then(...).query is not a function'',而没有客户端的情况类似:“TypeError: pool.query(...).then(...).catch(...).query is not a function”
标签: node.js postgresql node-postgres