【问题标题】:Error when using to_timestamp in node-postgres insert statement在 node-postgres insert 语句中使用 to_timestamp 时出错
【发布时间】:2021-05-28 17:07:30
【问题描述】:

我正在使用 node-postgres 并尝试使用 to_timestamp 并收到错误:

ReferenceError: to_timestamp is not defined

这是我的代码:

let sDate = format(parseISO(myStartDate),'dd/MM/yyyy')
let sTime = format(parseISO(myStartTime), 'HH:mm:ss')
let sDateTime = `${sDate} ${sTime}`
let eDate = format(parseISO(myEndDate),'dd/MM/yyyy')
let eTime = format(parseISO(MyEndTime), 'HH:mm:ss')
let eDateTime = `${eDate} ${eTime}`

console.log(sDate, sTime, sDateTime)
console.log(eDate, eTime, eDateTime)

data = await pool.query(        
      "INSERT INTO my_table (window_start, window_end ) VALUES($1, $2) RETURNING data_id",
          [ to_timestamp(sDateTime,'YYYY-MM-DD HH24:MI:SS'), to_timestamp(eDateTime,'YYYY-MM-DD HH24:MI:SS') ]
    );

这是我的控制台输出:

25/05/2021 19:56:40 25/05/2021 19:56:40
25/05/2021 19:56:40 25/05/2021 19:56:40

但随后ReferenceError: to_timestamp is not defined 出现错误

请注意,window_start 和 window_end 的表格列的类型均为 timestamp

有什么想法吗?

【问题讨论】:

    标签: node.js postgresql node-postgres


    【解决方案1】:

    参数没有插入到SQL语句中,而是在Javascript中执行to_timestamp函数,导致错误。

    使用类似的东西

    data = await pool.query(        
          "INSERT INTO my_table (window_start, window_end) VALUES (to_timestamp($1, 'YYYY-MM-DD HH24:MI:SS'), to_timestamp($2, 'YYYY-MM-DD HH24:MI:SS') RETURNING data_id",
              [ sDateTime, eDateTime ]
        );
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-03-20
      • 2016-10-05
      • 1970-01-01
      • 2012-06-26
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多