sql - Node.js PostgreSQL

标签 sql node.js postgresql

我有一个问题,如何从 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();

最佳答案

因为你还没有使用池,我假设你使用的是 6 岁以上的 pg。你应该这样做:

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

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

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

关于sql - Node.js PostgreSQL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49465395/

相关文章:

javascript - 当在 Node.js 中调用我的函数之一时,程序首先调用 emitHookFactory 并陷入无限循环

node.js - AWS Lambda 函数可以调用另一个函数吗

sql - 如何在 postgresql 中调试 "division by zero"错误

mysql - SQL-Joining Column values = Column Name 上的两个表

node.js - windows下用nodejs抓包

sql - 如何更改 PostgreSQL 表中列的数据类型?

Django 注释平均查询不显示小数位

php - 使用Join查询两个表

mysql - 检查开始日期或结束日期是否在上周

sql - 使用脚本将 Excel 工作表放入临时表中