【问题标题】:SequelizeDatabaseError: invalid input syntax for type ti mestamp with time zone: "Invalid date"SequelizeDatabaseError:带有时区的时间戳类型的无效输入语法:“无效日期”
【发布时间】:2022-02-02 08:36:05
【问题描述】:

我正在使用 Postgres,但我不知道如何解决这个问题: UnhandledPromiseRejectionWarning: SequelizeDatabaseError: invalid input syntax for type timestamp with time zone: "Invalid date" 这是哪里的错误,?以及如何克服这个错误?

【问题讨论】:

  • 把你的代码也放上去

标签: node.js sequelize.js


【解决方案1】:

将日期数据类型更改为使用date 而不是timestamp

【讨论】:

    【解决方案2】:

    您在查询中的where 属性必须是数组,而不是对象。

    {
      where: [{
        code: barcode.barcode,
        createdAt: { [Op.gt]: new Date(new Date() - 24 * 60 * 60 * 1000) }
      }],
      defaults: {code: barcode.barcode}
    }
    

    【讨论】:

      【解决方案3】:

      从日期中删除时区。

      您可以使用toISOString() 来实现这样的目的

      new Date().toISOString().slice(0, 19).replace('T', ' ');
      

      例如

      const date = new Date(new Date() - 24 * 60 * 60 * 1000);
      const dateString = date.toISOString().slice(0, 19).replace('T', ' ');
      
      
      User.findAll({
        where: {
           createdAt: { [Op.gt]: dateString }
        }
      })
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2020-11-05
        • 2019-02-11
        • 1970-01-01
        • 2021-05-06
        • 2020-11-13
        • 2012-12-24
        • 2020-03-30
        相关资源
        最近更新 更多