How do I read an objects key in an indexeddb autoincrement objectstore, given an index

自古美人都是妖i 提交于 2019-12-25 02:13:05

问题


This question in code form, I can use .put(doc, key) to overwrite the object, but I cant figure out how to read its 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);
  };
}

回答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 is in fact the key! But only as a response to put or add, not a get. You can use this value to put() the data in the future.

With out of line keys, like you have: objectstore.put(data, key)

With inline keys you can just objectstore.put(data)

Update: The problem is you're looping writeDocs but in your first loop key is null so it throws an error.

Here's a working example.

Second Update: It looks like some code may have been added, or I missed something earlier. In addition to what I have flagged above, but your index is unique and you're using the same my_index value twice, hence the error on second put(). I avoided this problem in my fiddle solution by making my_index unique each pageload.




回答2:


Edited: The plain solution for this is to use index.getKey(indexVal), switching to inline keys also worked, but would lead to a messy migration. changing db.createObjectStore('some_store_name', {autoIncrement : true}).createIndex('my_index', 'my_index', {unique: true}); to 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);
  };
}


来源:https://stackoverflow.com/questions/22562710/how-do-i-read-an-objects-key-in-an-indexeddb-autoincrement-objectstore-given-an

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!