【问题标题】:NodeJS PostgreSQLNodeJS PostgreSQL
【发布时间】:2018-03-26 05:16:57
【问题描述】:

我有一个问题,如何从 postgresql 输出所有行。现在我有一些错误。请帮我。谢谢。

这是我的代码:

'use strict'

const res = client.query("SELECT * FROM public", function(err, rows, fileds) {
const row = [];
for(let i=0; i<rows.length; i++) {
    row = rows[i];
    console.log(row);
}
rows.forEach(async function(row) {
    console.log(row.name);
})
console.log('Finish');
});
const func = ms => new Promise(res => setTimeout(res, ms));
console.dir({func});
console.dir(res);
client.end();

【问题讨论】:

  • 错误是什么?
  • @parladneupane 现在我改变了它。它显示了{ func: [Function: func] }undefined

标签: sql node.js postgresql


【解决方案1】:

由于您还没有使用 pool,我假设您使用的 pg 早于 6。您应该这样做:

return client.query(sqlStatement)
    .then(res =>
    {
        client.end();

        return res.rows;
    })
    .catch(e =>
    {
        client.end();

        console.error(e);
        throw e;
    });

【讨论】:

  • 你用什么连接postgres,什么版本?
  • 查看 PostgreSQL,我创建了数据库银行业务,并在其中有一个表帐户的模式。我的 pg 版本是 7.4.1。那么如何输出这个表呢?
  • 我不知道为什么控制台向我显示query.on 的错误
  • Lado 我刚刚用 pg@7 格式更改了代码。
  • 酷。谢谢
猜你喜欢
  • 2020-06-18
  • 2018-11-01
  • 1970-01-01
  • 2017-10-23
  • 1970-01-01
  • 2014-04-13
  • 2017-10-21
  • 2016-03-07
  • 1970-01-01
相关资源
最近更新 更多