javascript - IndexedDB : restrictions? 中的无效键路径

标签 javascript indexeddb key-value-store

我正在尝试使用一些 JavaScript 创建一个非常简单的 IndexedDB,但它已经在 on 处理程序中失败了。显然浏览器 (Chrome 57) 无法 parse我存储的 keyPath(在 Basic Concepts 中)。

我或多或少地遵循了这些简单的例子:MDNOpera-Dev .

假设我想在数据库中存储这样的对象:

{
    "1": 23, // the unique id
    "2": 'Name',
    "3": 'Description',
    "4": null,
    "5": null
}

代码如下:

var sStoreNodes = 'nodes';
var sIdFieldNode = '1'; // the important part

// event is fired for creating the DB and upgrading the version
request.onupgradeneeded = function(event)
{
    var db = event.target.result;

    // Create an objectStore for nodes. Unique key should be the id of the node, on property 1. 
    // So ID will be the key!
    var objectStore =  db.createObjectStore(
        sStoreNodes,
        {
            // changing to a plain string works, if it is a valid identifier and not just a strigified number
            'keyPath'       : [ sIdFieldNode ],
            'autoIncrement' : false // really important here
        });
};

错误信息如下:

Uncaught DOMException: Failed to execute 'createObjectStore' on 'IDBDatabase': The keyPath option is not a valid key path. at IDBOpenDBRequest.CCapIndexedDB.request.onupgradeneeded

我也可以尝试省略键路径,但我想知道为什么会发生这种情况,如果我真的需要使用(复杂的)键路径,我想知道我能做些什么吗。

关于规范:

我不确定,是否 this可以在这里应用:

A value is said to be a valid key if it is one of the following ECMAScript [ECMA-262] types: Number primitive value, String primitive value, Date object, or Array object.

什么this实际上意味着:

If the key path is a DOMString, the value [for getting the key path] will be a DOMString equal to the key path. If the key path is a sequence, the value will be a new Array, populated by appending Strings equal to each DOMString in the sequence.


编辑 如果您不使用字符串化数字,而是使用字符串作为有效标识符(以字符 [a-zA-Z] 开头),则此方法有效。所以 'keyPath' : 'b' 没问题。我猜这是因为这个值用于创建像 a.b.c 这样的路径。

最佳答案

Here is the definition of a key path, from the spec:

A key path is a DOMString or sequence that defines how to extract a key from a value. A valid key path is one of:

  • An empty DOMString.
  • An identifier, which is a DOMString matching the IdentifierName production from the ECMAScript Language Specification [ECMA-262].
  • A DOMString consisting of two or more identifiers separated by periods (ASCII character code 46).
  • A non-empty sequence containing only DOMStrings conforming to the above requirements.

对于包含整数的字符串,显然第一个、第三个和第四个选项不适用。对于第二个,我们必须看到 what an IdentifierName is , 这有点复杂,但基本上它必须以字母、下划线或美元符号开头。这意味着仅包含整数的字符串不是有效的键路径。

如果您确实有一个对象,其中主键位于名称为包含整数的字符串的字段中,您可以重命名该字段或不使用键路径(在这种情况下,您必须手动将键指定为IDBObjectStore.addIDBObjectStore.put 的第二个参数。

您链接到 the definition for a key ,它定义了键可以具有的有效值(例如对象 {a: 1},其中键路径为 'a',键为 1 ,这是有效的)。

other thing you linked to是关于关键路径,例如 a.b.c 引用 {a: {b: {c: 1}}}

关于javascript - IndexedDB : restrictions? 中的无效键路径,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43273983/

相关文章:

javascript - React 向下传递钩子(Hook)会导致重新渲染并失去对输入的关注

javascript - 从indexeddb分段加载数据

caching - memcached 与基于数据库的键值表?

database - 键值存储中的反向索引和数据建模

javascript - react : Iterating through deeply nested objects and displaying them without tearing my hair out

javascript - 导入浏览器实现的默认样式

javascript - 在包含(文字连接)中使用文字与 Sequelize

javascript - IndexedDB 与 iOS 8/Safari

indexeddb - 如何在另一个页面上重新打开现有的indexedDB数据库

memcached - 是否存在可以将更改通知外部实体的内存中键/值存储?