在IE 10/11中使用数组作为indexedDB keyPaths

Jos*_*osh 6 javascript internet-explorer indexeddb

我有一个数组作为我的keypath使用indexedDB,它适用于Chrome和Firefox,但当我尝试使用IE添加/放置时,它给我一个DataError.

var request = window.indexedDB.open("MyTestDatabase");

request.onsuccess = function(event) {
    var database = event.target.result;
    var transaction = database.transaction(["document"], "readwrite");
    var objectStore = transaction.objectStore("document");
    var request = objectStore.put({title: 'MyDoc', version: 0});
    request.onsuccess = function() {
        console.log('document added');
    };
    request.error = function(error) {
        console.log(JSON.stringify(error));
    };                
    transaction.oncomplete = function() {
        console.log('transaction complete');
    };
    transaction.onerror = function(error) {
        console.log(JSON.stringify(error));
    };
};

request.onupgradeneeded = function(event) {
    event.target.result.createObjectStore("document", {keyPath: ['title', 'version']});
};
Run Code Online (Sandbox Code Playgroud)

错误截图:

在此输入图像描述

如何保留我的双keyPath并使其与IE一起使用?

Jos*_*osh 2

目前解决这个问题的唯一方法是从数组中创建一个字符串,并在键处使用它,正如 Kyaw 提到的那样。

request.onupgradeneeded = function(event) {
    event.target.result.createObjectStore("document", {keyPath: ['id']});
};
Run Code Online (Sandbox Code Playgroud)

示例中的 id 现在必须是包含标题和版本的串联字符串。有些喜欢:MyDoc_0。不过,您仍然可以存储标题和版本字段,这样您就可以轻松访问它们,而无需拆分 id 字符串。