我正在尝试向对象存储中添加一些值,以得到此错误作为回报(第22行)。对象库使用keyPath'id'和autoincrement:true。
function cacheObject(storeName, object) {
return new Promise(function(resolve, reject) {
let transaction = db.transaction([storeName], "readwrite");
transaction.oncomplete = function(event) {
console.log("Transaction complete");
resolve(object);
};
transaction.onabort = function(event) {
console.log("Transaction aborted: " + event);
reject();
};
transaction.onerror = function (event) {
console.log("Transaction error: "+ event.toString());
reject();
};
let objectStore = transaction.objectStore(storeName);
let objectReq = objectStore.add(object);
objectReq.onsuccess = function (event) {
console.log("saved object to idb: " + JSON.stringify(object));
};
});
}
以下是onupgradeneeded
事件的示例:
req.onupgradeneeded = function (e) {
let db = e.target.result;
console.log("Running idb onupgradeneeded");
if (!db.objectStoreNames.contains(STORY_STORE_NAME)) {
let storyStore = db.createObjectStore(STORY_STORE_NAME, {keyPath: "id", autoIncrement: true});
storyStore.createIndex('picture', 'picture', {unique: false});
storyStore.createIndex('text', 'text', {unique: false});
//more indexes created here
答案 0 :(得分:0)
我找到原因和解决方法
简而言之,store.add
值不是object({}),因此object.id = autoGeneratedId
会引发错误
我使用的是
database.createObjectStore(storeName, {
keyPath: 'id',
autoIncrement: true
})
并使用交易获取存储
store2.add(1)
store2.add(2)
1.id =“ val”会引发错误
当我将其更改为store2.add({k: "v"})
时,它会修复