gpt4 book ai didi

javascript - 无法读取 Node 和 Postgresql 中未定义的属性 'query'

转载 作者:行者123 更新时间:2023-12-03 02:14:06 24 4
gpt4 key购买 nike

const pool = new pg.Pool(config);
var tablename = req.body.tablename;

pool.connect(function (err, client, done) {
var query_get_value = 'SELECT * FROM '+ tablename;
client.query(query_get_value, function (err, result) {
done();
if (err) {
throw err;
}
var rows = result.rows;
.....

this code runs correctly for a while, but a few seconds later, I received following errors:(I call this function repeatedly on the view side using setInterval function)

 client.query(query_get_value, function (err, result) {
^

TypeError: Cannot read property 'query' of undefined
at /var/node_project/controllers/index.js:630:10
at client.connect (/var/node_project/node_modules/pg-pool/index.js:219:9)
at Connection.connectingErrorHandler (/var/node_project/node_modules/pg/lib/client.js:123:14)
at Connection.emit (events.js:127:13)
at Socket.<anonymous> (/var/node_project/node_modules/pg/lib/connection.js:117:12)
at Socket.emit (events.js:127:13)
at addChunk (_stream_readable.js:269:12)
at readableAddChunk (_stream_readable.js:256:11)
at Socket.Readable.push (_stream_readable.js:213:10)
at TCP.onread (net.js:598:20)

最佳答案

您可能会收到错误,因此client 没有方法query,因为它未定义。

您可以轻松地将其包装在 if/else block 中以确保客户端已设置:

pool.connect(function (err, client, done) {
if(err) {
console.log(err)
}else{
var query_get_value = 'SELECT * FROM '+ tablename;
client.query(query_get_value, function (err, result) {
done();
if (err) {
throw err;
}
var rows = result.rows;
}

关于javascript - 无法读取 Node 和 Postgresql 中未定义的属性 'query',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49433629/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com