在给定索引的情况下,如何读取indexeddb自动增量对象库中的对象键

时间:2014-03-21 15:19:30

标签: html5 indexeddb

这个代码形式的问题,我可以使用.put(doc, key)来覆盖对象,但我无法弄清楚如何阅读它的密钥

// After writing doc1 to an autoincrement
// objectStore, I want to overwrite it 
// later, I only know its index 
// (not its key)

var docs = [
  {'a':'doc1', 'my_index': 'anindex'},
  {'a':'doc2', 'my_index': 'anindex'}
];

indexedDB.deleteDatabase('foo').onsuccess = function () { 

  var idb;
  var req = indexedDB.open('foo');

  function writeDocs(docs) {

    if (!docs.length) { 
      console.log('done writing');
      return;
    }

    var txn = idb.transaction(['some_store_name'], 'readwrite');
    var doc = docs.shift();
    console.log('writing', doc);

    var get = txn.objectStore('some_store_name').index('my_index')
      .get(doc.my_index);

    get.onsuccess = function(e) { 

      var key = null;

      if (e.target.result) { 
        // If there is a result here, I want to overwrite it
        // to do that I need its key, cant find it here?    
        console.log(e.target.result);
      }

      var dataReq = txn.objectStore('some_store_name').put(doc);

      dataReq.onsuccess = function (e) {
        console.log('wroted', e.target.result);
        writeDocs(docs);
      }

      dataReq.onerror = function () { 
        console.log('it broke');
        writeDocs(txn, docs);
      }
    }
  };

  req.onupgradeneeded = function(e) {
    var db = e.target.result;
    db.createObjectStore('some_store_name', {autoIncrement : true})
      .createIndex('my_index', 'my_index', {unique: true});
  };

  req.onsuccess = function(e) { 
    idb = e.target.result;
    writeDocs(docs);
  };
}

2 个答案:

答案 0 :(得分:1)

  if (e.target.result) {
    // If there is a result here, I want to overwrite it
    // to do that I need its key, cant find it here?    
    console.log(e.target.result);
  }

e.target.result实际上是关键!但仅作为对putadd的回复,而非get。您可以将此值用于put()以后的数据。

使用外线密钥,就像您拥有:objectstore.put(data, key)

一样

使用内嵌键,您只需objectstore.put(data)

更新:问题是您正在循环writeDocs,但在第一个循环中keynull,因此会引发错误。

Here's a working example

第二次更新:看起来某些代码可能已添加,或者我之前遗漏了一些内容。除了我上面标记的内容,但您的索引为unique并且您使用相同的my_index值两次,因此第二个put()上的错误。我通过使每个页面加载my_index唯一,在我的小提琴解决方案中避免了这个问题。

答案 1 :(得分:-1)

编辑:对此的简单解决方案是使用index.getKey(indexVal),切换到内联键也有效,但会导致混乱的迁移。将db.createObjectStore('some_store_name', {autoIncrement : true}).createIndex('my_index', 'my_index', {unique: true});更改为db.createObjectStore('some_store_name', {keyPath: 'seq', autoIncrement : true}).createIndex('my_index', 'my_index', {unique: true});

// After writing doc1 to an autoincrement
// objectStore, I want to overwrite it 
// later, I only know its index 
// (not its key)

var docs = [
  {'a':'doc1', 'my_index': 'anindex'},
  {'a':'doc2', 'my_index': 'anindex'}
];

indexedDB.deleteDatabase('foo').onsuccess = function () { 

  var idb;
  var req = indexedDB.open('foo');

  function writeDocs(docs) {

    if (!docs.length) { 
      console.log('done writing');
      return;
    }

    var txn = idb.transaction(['some_store_name'], 'readwrite');
    var doc = docs.shift();

    var get = txn.objectStore('some_store_name').index('my_index')
      .get(doc.my_index);

    get.onsuccess = function(e) {

      if (e.target.result) { 
        doc.seq = e.target.result.seq;
      }
      var dataReq = txn.objectStore('some_store_name').put(doc);

      dataReq.onsuccess = function (e) {
        console.log('wroted', e.target.result);
        writeDocs(docs);
      }

      dataReq.onerror = function () { 
        console.log('it broke');
        writeDocs(txn, docs);
      }
    }
  };

  req.onupgradeneeded = function(e) {
    var db = e.target.result;
    db.createObjectStore('some_store_name', {keyPath: 'seq', autoIncrement : true})
      .createIndex('my_index', 'my_index', {unique: true});
  };

  req.onsuccess = function(e) { 
    idb = e.target.result;
    writeDocs(docs);
  };
}