IndexedDB 中的无效键路径:限制?
Invalid key path in IndexedDB: restrictions?
我正在尝试用一些 JavaScript 创建一个非常简单的 IndexedDB,但它已经在 on 处理程序中失败了。显然浏览器 (Chrome 57) 无法 parse the keyPath
(in Basic Concepts) 我的存储。
我或多或少地遵循了这些简单的例子:MDN or Opera-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.
Edit 如果您不使用字符串化数字,而是使用字符串,这是有效的标识符(以字符 [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,有点复杂,但基本上必须以字母、下划线或美元符号开头。这意味着仅包含整数的字符串不是有效的键路径。
如果您确实有一个对象,其中主键位于名称为包含整数的字符串的字段中,您可以重命名该字段或不使用键路径(在这种情况下,您必须手动指定key 作为 IDBObjectStore.add
和 IDBObjectStore.put
).
的第二个参数
您链接到 the definition for a key,它定义了键可以具有的有效值(例如对象 {a: 1}
,其中键路径是 'a'
,键是 1
, 这是有效的).
other thing you linked to 是关于关键路径,例如 a.b.c
引用 {a: {b: {c: 1}}}
。
我正在尝试用一些 JavaScript 创建一个非常简单的 IndexedDB,但它已经在 on 处理程序中失败了。显然浏览器 (Chrome 57) 无法 parse the keyPath
(in Basic Concepts) 我的存储。
我或多或少地遵循了这些简单的例子:MDN or Opera-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.
Edit 如果您不使用字符串化数字,而是使用字符串,这是有效的标识符(以字符 [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,有点复杂,但基本上必须以字母、下划线或美元符号开头。这意味着仅包含整数的字符串不是有效的键路径。
如果您确实有一个对象,其中主键位于名称为包含整数的字符串的字段中,您可以重命名该字段或不使用键路径(在这种情况下,您必须手动指定key 作为 IDBObjectStore.add
和 IDBObjectStore.put
).
您链接到 the definition for a key,它定义了键可以具有的有效值(例如对象 {a: 1}
,其中键路径是 'a'
,键是 1
, 这是有效的).
other thing you linked to 是关于关键路径,例如 a.b.c
引用 {a: {b: {c: 1}}}
。