1

我正在使用jquery-indexed-db 插件制作一个示例项目,只是为了学习indexeddb

JS代码

$(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("");

    });

});

代码

<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)

4

1 回答 1

1

您需要先创建事务,然后才能打开对象存储。

$.indexedDB(dbName).transaction([osName], 1).then(function(transaction){
    objectStore = transaction.objectStore(osName);
});

顺便说一句,那是您提供的完整脚本吗?我错过了创建对象存储的部分。

于 2012-11-03T06:29:53.997 回答