为什么使用linq2indexedDB插入IndexedDB失败,以及如何检索错误

Why is this insert into IndexedDB using linq2indexedDB failing, and how can I retrieve the error?

本文关键字:何检索 检索 错误 linq2indexedDB 插入 IndexedDB 失败 为什么      更新时间:2023-09-26

我使用linq2indexedDB包装器与IndexedDB一起工作。我正在使用TypeScript,但我不认为这真的与问题相关——它应该只是解释了稍微奇怪的语法。我创建了我的DB,并添加了一些对象存储和索引(代码缩短以保持重点):

var databaseDefinition = [{
        version: config.version,
        objectStores: [
            { name: "Regions", objectStoreOptions: { autoIncrement: false, keyPath: "ID" } },
            { name: "Countrys", objectStoreOptions: { autoIncrement: false, keyPath: "ID" } }
            // etc.
        ],
        indexes: [
            { objectStoreName: "Countrys", propertyName: "RegionID", indexOptions: { unique: true, multirow: false } }
            // etc.
        ]
    }];

这工作,并通过检查我的数据库结构在chrome F12工具,我可以看到预期的objectStores。然后尝试一个简单的插入操作:

    var region = serverData.Regions[0];
    this.db.linq.from("Regions").insert(region, region.ID).then((args) => {
        tracer.Trace("Insert success: " + args);
    }, (args) => {
        tracer.Trace("Insert failure: " + args);
    });

这失败了,但是返回的args对象是未定义的,所以我不知道为什么。谁能解释一下问题可能是什么,我该如何追踪它?

EDIT(删除了额外的代码,因为它与问题不严格相关。正如Kristof在下面的回答中所描述的那样,打开日志记录有助于追踪问题,并在下面我自己的回答中解决了这个问题)。

不能100%确定问题是什么,但是当定义了密钥并且对象中存在密钥的值时,不需要提供密钥。

我看不到你所有的代码,但是你是否创建了一个linq2indexeddb对象的新实例?

如果你做了,你可以这样做:

var db = window.linq2indexedDB("dbName", dbConfig, true);

最后一个参数将启用日志记录,所有日志结果将在控制台输出中打印。

另一种方法是

linq2indexedDB.prototype.utilities.debug(true);

如果您对linq2indexeddb有任何其他问题,请告诉我。我写的;)

这原来是我使用的linq2indexedDB版本中的一个小问题。IDBTransaction.READ_ONLYIDBTransaction.READ_WRITE已被弃用(参考文献)。快速搜索和替换,分别用字符串'readonly''readwrite'替换上述内容,为我解决了这个问题。

Kristof提到可能也有一个shim来处理这个问题,但我似乎没有从codeplex下载的最新版本。