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

    });

});

HTMLコード

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

私は何か間違ったことをしていますか?

Google Chrome(バージョン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 に答える