admin管理员组

文章数量:1391951

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

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

Share Improve this question edited May 24, 2023 at 9:41 djvg 14.4k7 gold badges84 silver badges116 bronze badges asked Aug 29, 2012 at 16:19 JohanJohan 35.2k62 gold badges189 silver badges306 bronze badges 2
  • What browser are you using? Also, I know this is not the easiest thing in the world with IndexedDB, but if you can post a self-contained test case that exhibits this problem, you'll have a better chance of getting a good answer. – dumbmatter Commented Aug 29, 2012 at 18:59
  • answered in this ment – djvg Commented May 24, 2023 at 9:45
Add a ment  | 

3 Answers 3

Reset to default 4

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

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

I do as follow on http://dev.yathit./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.

There are so many such errors incurred.Only this article http://dev.opera./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.onplete = 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);");
        };

    });

本文标签: