无法将数据添加到indexeddb对象存储 [英] Unable to add data to indexeddb objectstore

查看:213
本文介绍了无法将数据添加到indexeddb对象存储的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am using the jquery-indexed-db plugin to make a sample project just to go about learning 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>

单击提交按钮时出现异常.错误如下

I am getting an exception when i click the submit button. The error is as follows

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)

I am using Google Chrome (Version 22.0.1229.94)

推荐答案

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

You need to create a transaction before you can open an object store.

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

顺便说一句,这是您提供的完整脚本吗?我缺少创建对象存储的部分.

Btw is that the complete script you gave? I'm missing the part where you create an object store.

这篇关于无法将数据添加到indexeddb对象存储的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆