IndexedDB错误:尝试对不允许突变的数据库进行突变操作

时间:2012-08-29 16:19:59

标签: html5 indexeddb

console.log(db); //db object exists
console.log(db.objectStoreNames.contains('test')); //true - object store exists

var transaction = db.transaction(['test'], 'readwrite'); // this line is causing the error

A mutation operation was attempted on a database that did not allow mutations." code: "6

为什么我收到此错误?我的db和objectstore存在吗?我在失去理智! :D非常感谢任何帮助!

由于

2 个答案:

答案 0 :(得分:1)

我现在尝试在Mac OS x上使用chrome,safari,但没有发现任何错误。

我按照http://dev.yathit.com/ydn-db/using/schema.html(页面加载ydn.db.Storage对象)进行操作

schema = {stores: [{name: 'test'}]}
st = new ydn.db.Storage('test1', schema)
// ... wait for async
db = st.db()
db.transaction(['test'], 'readwrite')

旧的Chrome使用1代替'readwrite',但我不认为这是一个原因。

答案 1 :(得分:0)

发生了很多这样的错误。只有这篇文章http://dev.opera.com/articles/introduction-to-indexeddb/说明了为什么----- " 使用IndexedDB,我们数据库中的每个操作或事务都必须在回调函数中发生。"

这是我的简单示例,请使用chrome的devtool-> resource-> indexedDB进行检查;(如果indexedDB中没有任何内容,请尝试刷新浏览器)

html部分:

<form id="form1">
    <label for="task">What do you need to do?</label>
    <input type="text" name="task" id="task" value="" required>
    <button type="submit" id="submit">Save entry</button>
</form>

脚本部分:

var idb = indexedDB.open('niangzi10', 2);
    var dbobject; // Define a global variable to hold our database object
    idb.onsuccess = function (evt) {
        console.log("success");
        if (dbobject === undefined) {
            dbobject = evt.target.result;
        }
    }
    idb.onupgradeneeded = function (evt) {
        dbobject = evt.target.result;
        if (evt.oldVersion < 1) {
            dbobject.createObjectStore('tasks', { autoIncrement: true });
        }
    }
    //transaction operation in callback function
    var form1 = document.getElementById('form1');
    form1.addEventListener('submit', function (evt) {
        'use strict';
        evt.preventDefault();

        var entry = {}, transaction, objectstore, request;
        entry = { name: document.querySelector("#task").value };

        // Open a transaction for writing
        transaction = dbobject.transaction(['tasks'], 'readwrite');
        objectstore = transaction.objectStore('tasks');
        // Save the entry object
        request = objectstore.add(entry);
        transaction.oncomplete = function (evt) {
            alert("'" + document.querySelector("#task").value + "'has been insert into indexedDB;please check it using chrome's devtool->resource->indexedDB(if nothing,refresh browser);");
        };

    });