我正在使用jquery-indexed-db plugin制作示例项目,以便学习indexeddb。
JS CODE
$(function() {
/*Loggers*/
write = function(info) {
console.info(info);
}
writeError = function(e) {
console.info(e);
}
/*Settings*/
dbName = "testDB";
osName = "list";
/*DB Init*/
db = $.indexedDB(dbName).then(write, writeError)
/*ObjectStore Init*/
objectStore = db.objectStore(osName, false);
/*Adding a new record*/
$("#submit").click(function(){
objectStore.add(
{
"Name": $("#name").val(),
"Age": $("#age").val()
},
$("#id").val()
).then(write, writeError);
$("#id").val("");
$("#name").val("");
$("#age").val("");
});
});
HTML CODE
<ul>
<li>
<input type="text" id="id" placeholder="Id" />
</li>
<li>
<input type="text" id="name" placeholder="Name" />
</li>
<li>
<input type="text" id="age" placeholder="Age" />
</li>
<li>
<input type="button" id="submit" name="submit" value="Submit"/>
</li>
</ul>
点击提交按钮后,我收到异常。错误如下
IDBDatabaseException
code: 11
message: "InvalidStateError: DOM IDBDatabase Exception 11"
name: "InvalidStateError"
stack: "Error: An operation was called on an object on which it is not allowed or at a time when it is not allowed.↵ at null.<anonymous> (file:///*****/js/jquery.indexeddb.js:468:33)↵ at n (file:///******/js/jquery.js:1:14837)↵ at Object.o.add [as done] (file:///******/js/jquery.js:1:15052)↵ at Object.h.then (file:///******/js/jquery.js:1:16026)↵ at Object.<anonymous> (file:///******/js/jquery.indexeddb.js:465:17)↵ at Function.f.extend.Deferred (file:///******/js/jquery.js:1:16742)↵ at Object.$.extend.transaction (file:///******/js/jquery.indexeddb.js:464:15)↵ at Object.<anonymous> (file:///******/js/jquery.indexeddb.js:516:11)↵ at Function.f.extend.Deferred (file:///******/js/jquery.js:1:16742)↵ at op (file:///******/js/jquery.indexeddb.js:501:16)" type: "exception"
我做错了吗?
我使用的是谷歌浏览器(版本22.0.1229.94)
答案 0 :(得分:1)
您需要先创建一个事务,然后才能打开对象库。
$.indexedDB(dbName).transaction([osName], 1).then(function(transaction){
objectStore = transaction.objectStore(osName);
});
顺便说一句,你提供的完整脚本是什么?我错过了创建对象库的部分。