【问题标题】:Response rows: [ anonymous { today: 2017-08-14T00:08:52.643Z } ] - Node version v8.2.1响应行:[匿名{今天:2017-08-14T00:08:52.643Z}] - 节点版本 v8.2.1
【发布时间】:2021-12-30 00:11:53
【问题描述】:

我正在使用 node-postgres 并尝试制作第一个示例

今天用一个方法创建一个名为 Postgres 的类

const conn = new pg.Pool({
  user: 'user',
  host: 'localhost',
  database: 'test',
  password: 'pass',
  port: 5432,
});

class Postgres {
   today() {
    conn.query("SELECT NOW() as today",(err,res)=>{
        if (err) throw err
            //console.log(JSON.stringify(res.rows[0]));
            return JSON.stringify(res.rows[0]);
            conn.end();
    })

    }
}

结果是 rows: [ anonymous { today: 2017-08-14T00:08:52.643Z } ] 并尝试使用 JSON.stringify(res.rows[0]) 但不工作我无法获取该字段

Result {
  command: 'SELECT',
  rowCount: 1,
  oid: NaN,
  rows: [ anonymous { today: 2017-08-14T00:08:52.643Z } ],
  fields:
   [ Field {
       name: 'today',
       tableID: 0,
       columnID: 0,
       dataTypeID: 1184,
       dataTypeSize: 8,
       dataTypeModifier: -1,
       format: 'text' } ],
  _parsers: [ [Function: parseDate] ],
  RowCtor: [Function: anonymous],
  rowAsArray: false,
  _getTypeParser: [Function: bound ] }

不胜感激

【问题讨论】:

    标签: node.js anonymous-function node-postgres


    【解决方案1】:

    试试res.rows[0].today这会奏效!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2010-10-04
      • 1970-01-01
      • 2019-12-15
      • 2018-02-17
      • 2019-01-30
      • 1970-01-01
      • 2021-11-01
      • 2017-11-05
      相关资源
      最近更新 更多