首页 > 解决方案 > 无法使用 knes 从 postgres 获取当前时间戳

问题描述

我正在使用 knex 并使用原始查询来获取 postgres 数据库的当前时间戳。我在用

knex.raw('select now()').then(function(resp)
{
 console.log(resp)
})

但我无法得到想要的结果。请告诉我能做什么的解决方案。

标签: sqlnode.jspostgresqldatetimeknex.js

解决方案


这应该这样做:

knex.select(knex.fn.now()).then(res => console.log(res[0].now));

也许你也应该告诉你想要的结果是什么。

编辑:编辑查询以实际从响应中获取行和列,因为select CURRENT_TIMESTAMP;在 postgres 上返回以下内容:

mikaelle=# select CURRENT_TIMESTAMP;
              now              
-------------------------------
 2018-12-10 14:48:01.472945+02
(1 row)

推荐阅读