【发布时间】: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