You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何使用JS在IndexedDB的DateTime列中插入数据?

无法向jsStore的TEST表插入记录——DateTime列格式问题

我的表结构如下:

{
  name : "TEST",
  columns : {
    K_A : {dataType : "string"},
    DT_VAL : {dataType : "date_time"},
    DT_SENT : {dataType : "date_time"},
    pk: {
      primaryKey: true,
      keyPath: ['K_A', 'DT_VAL']
    },
    sent: {
      keyPath : ["DT_SENT"]
    }               
  }
}

我完全没法往这个表里插入记录,搞不清DateTime列的正确格式。试了new Date()、Date.now()、new Date().toISOString()、new Date().toGMTString()、new Date().toUTCString()、new Date().toString()这些方式,全都没用。

我尝试的代码:

dt=new Date().toISOString();
record={
  K_A:"A", 
  DT_VAL: dt,
  DT_SENT: dt 
};
    
// 使用jsStore
db.connection.insert({
  into: "TEST",
  upsert:true, // 原代码此处缺少逗号,会引发语法错误
  values : record,
  validation: false
});

不管试哪种方式,都会抛出如下错误:

未捕获(在Promise中)
Object { message: "提供给操作的数据不符合要求。", type: "DataError" }

解决方法

jsStore的date_time类型要求必须传入原生Date对象,不能是字符串、时间戳这类格式。同时原代码中upsert:true后缺少逗号,这是语法错误,必须补上。

修正后的插入代码:

const dt = new Date(); // 直接使用原生Date对象
const record = {
  K_A:"A", 
  DT_VAL: dt,
  DT_SENT: dt 
};
    
db.connection.insert({
  into: "TEST",
  upsert:true,
  values : record,
  validation: false
});

调整后即可正常插入记录。

内容的提问来源于stack exchange,提问作者user2244705

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.13 22:23:25