Johan
Johan

Reputation: 35193

IndexedDB error: A mutation operation was attempted on a database that did not allow mutations

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

Why am i getting this error? My db and objectstore exists? I'm loosing my mind! :D Any help is much appreciated!

Thanks

Upvotes: 3

Views: 5545

Answers (3)

Josh Kelley
Josh Kelley

Reputation: 58372

One potential cause of this error: Firefox does not support IndexedDB in private browsing windows. See https://bugzilla.mozilla.org/show_bug.cgi?id=1639542 and https://github.com/jakearchibald/idb/issues/81.

Upvotes: 4

wengeezhang
wengeezhang

Reputation: 3101

There are so many such errors incurred.Only this article http://dev.opera.com/articles/introduction-to-indexeddb/ shows why----- " With IndexedDB, every operation or transaction on our database must occur within a callback function."

This is my simple example,please check it using chrome's devtool->resource->indexedDB;(if nothing in indexedDB,try refresh browser)

html section:

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

script section:

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

    });

Upvotes: 0

Kyaw Tun
Kyaw Tun

Reputation: 13131

I just try now on chrome, safari on Mac os x and found no error.

I do as follow on http://dev.yathit.com/ydn-db/using/schema.html (the page load ydn.db.Storage object)

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

old chrome use 1 instead 'readwrite', but i don't think it a reason.

Upvotes: 1

Related Questions