我想创建一个非常简单的IndexedDB,使用一些JavaScript,但在on处理程序中失败了。 显然,浏览器(Chrome 57)无法解析我的存储中
假设我想在数据库中存储像这样的对象:
错误信息如下:
关于规范:
我不确定this是否适用于此处:
如果键路径是DOMString,则获取键路径的值将是等于键路径的DOMString。如果键路径是序列,则该值将是一个新的数组,通过追加与序列中每个DOMString相等的字符串来填充。
如果你使用字符串而不是字符串化的数字,那么这个方法是可行的,因为字符串是一个有效的标识符(以字符[a-zA-Z]开头)。所以,
keyPath
(在基本概念中)。 我大致遵循这些简单的示例:MDN或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是否适用于此处:
并且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.
如果键路径是DOMString,则获取键路径的值将是等于键路径的DOMString。如果键路径是序列,则该值将是一个新的数组,通过追加与序列中每个DOMString相等的字符串来填充。
如果你使用字符串而不是字符串化的数字,那么这个方法是可行的,因为字符串是一个有效的标识符(以字符[a-zA-Z]开头)。所以,
'keyPath' : 'b'
是可以的。我猜这是因为这个值用来创建路径,比如 a.b.c
。