0
我创建了一组JSON数据,这些数据在升级事件期间传递到IndexedDB。它创建了一个非常好的IndexedDB数据库,看起来没有问题。问题是,当您将数据库从版本1升级到2时,它将覆盖所有现有数据,因为它只是通过JSON进行循环并应用它。IndexedDB在升级过程中覆盖值
在升级中必须有某种方法来防止没有唯一键的新数据库项写入任何内容。我将如何做到这一点?由于以下内容只会覆盖当前数据。
objectStore = event.currentTarget.transaction.objectStore('player');
objectStore.put({ key: 'non unique, but I replace my twin\'s data anyway', data: value });
在此先感谢帮助,搜索了一切,找不到解决此问题的方法。
编辑 2012年8月24日
按使用获得检查信息的克里斯托夫的建议,我做了一些挖掘和研究。找到答案后,我决定写一些伪代码来帮助你。
// Create the get request inside your upgrade
var tableData = event.currentTarget.transaction.objectStore('your table name');
var getData = tableStore.get('your key');
// Run the get request
getData.onsuccess = function (e) {
var result = e.target.result;
// Immediately exit if the current result is not undefined
if (result !== undefined) {
return;
}
// Note, you may or may not need a closure to write data in the callback
// See here for more info on closures in events http://stackoverflow.com/questions/3495679/passing-parameters-in-javascript-onclick-event#answer-3495722
tableStore.add('data to add');
};
尝试除get之外的所有选项。所以我应该做get.success ='不要写这个'; get.error ='写下这个';我猜如果它已经不存在,会引发一个令人讨厌的错误,但不是吗? –
以下是get.success = function(e){if(!get.result){// insert}}。这应该工作。我宁愿做我最后的选择。有可能数据库的版本也可以是“”...... –
如果我检查数据库及其0或没有,我只能升级一次。我需要这可以升级为可能的几个版本。想要了解一下这个周末是否存在某些关键的东西。 –