问题描述
我正在尝试使用IndexedDB在本地存储一些数据。下面我有一个简单的例子,我试图让onupgradeneeded事件触发
I'm trying to get some data stored locally using IndexedDB. Below I'm I have a simple example with which I'm trying to get the onupgradeneeded event to fire
<html>
<head>
<script>
var indexedDB = window.indexedDB || window.webkitIndexedDB
||window.mozIndexedDB||window.msIndexedDB;
var request = indexedDB.open("mydb",2),
customerData=[
{ssn:"444-44-4444",name:"Bill",age:35,email:"[email protected]"},
{ssn:"555-55-5555",name:"Donna",age:32,email:"[email protected]"}
];
request.onerror = function(event){
alert("ERROR") ;
};
request.onupgradeneeded = function(event) {
alert("UPGRADE NEEDED") ;
var objectStore = db.createObjectStore("customers",{keyPath:"ssn"});
objectStore.createIndex("name","name",{unique:false});
objectStore.createIndex("email","email",{unique:true});
for(var i in customerData){
objectStore.add(customerData[i]);
}
} ;
request.onsuccess = function(e) {
alert("SUCCESS") ;
} ;
</script>
</head>
</html>
我试图更改版本号但是我尝试onupgradeneeded的任何东西都不会被调用/触发。有什么建议吗?
I tried to change the version number but whatever I try onupgradeneeded is never called/fired. Any suggestions why ?
欢呼
卢卡
推荐答案
我在FF 10中对此进行了测试,它对我有用。 (直到代码尝试使用未定义的db变量。)
I tested this in FF 10 and it worked for me. (Until the code tried to use the undefined "db" variable".)
您使用的浏览器是什么?升级所需事件仅在FF 10中引发.Chrome 16仍然使用旧草稿,你必须检查db.version属性并调用db.setVersion,如果它不是你想要的。
What browser are you using? The upgradeneeded event is only raised in FF 10. Chrome 16 still uses the old draft where you have to check the db.version property and call db.setVersion if it's not what you want.
如果你使用FF10,你确定吗?还没有创建mydb吗?
If you are using FF10, are you sure that "mydb" isn't already created?
另外,请确保您通过真实的Web服务器进行测试.FF将不允许您使用indexedDB本地HTML文件。如果发生这种情况,您应该会在控制台中看到类似操作失败,原因与数据库本身无关且未被任何其他错误代码覆盖的错误。
Also, be sure that you're testing this through a real web server. FF won't let you use indexedDB with local HTML files. If that's happening, you should see an error like "The operation failed for reasons unrelated to the database itself and not covered by any other error code" in your console.
这篇关于为什么连接到indexedDB时从未调用过onupgradeneeded回调?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!