小编Mat*_*att的帖子

IndexedDB IDBObjectStore.add 错误:“生成的键无法插入到值中”

我试图向对象存储中添加一些值,但收到此错误(在第 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));
    };
  });
}
Run Code Online (Sandbox Code Playgroud)

以下是该活动的示例onupgradeneeded

  req.onupgradeneeded = …
Run Code Online (Sandbox Code Playgroud)

javascript indexeddb

5
推荐指数
1
解决办法
1892
查看次数

标签 统计

indexeddb ×1

javascript ×1