将对象添加到indexedDB对象库时的InvalidStateError

时间:2014-05-05 06:57:48

标签: javascript html5 indexeddb

我已经定义了IndexedDB和IDBTransaction

dbVersion = 1;
var db;
var dbreq;

var customerData = { ssn: "444", name: "Bill", age: 35, email: "bill@company.com"}

function create_openDB() 
{
alert('In DB function');

try {
dbreq = indexedDB.open("IndexDB", dbVersion);

dbreq.onsuccess = function (event) {
db = dbreq.result;
}

dbreq.onerror = function (event) {
console.log("indexedD Error: " + event.message);
}

dbreq.onupgradeneeded = function(event) {

var objectStore = event.target.result.createObjectStore("customers", {keyPath:"ssn"});

objectStore.createIndex("name", "name", { unique: false });
objectStore.createIndex("email", "email", { unique: true });

console.log("All object stores created");

createDatabaseObjects( event.target.result );
} 

}catch (e) {
console.log("Error: " + e.message);
}
}

我调用此函数,以便indexeddb可以没有问题,因为它异步工作。

function createDatabaseObjects( dbHandle ) {

try {

if ( dbHandle == null ) {
console.log( "Can't create database objects; the database is not open." );
} 
else 
{
console.log("in else");

它一直在努力直到这里。我可以在控制台中看到此消息。任何事情都会引发异常。

var transaction =  dbHandle.transaction(["customers"], "readwrite");
alert("after transaction made");


var objectStore = transaction.objectStore("customers");

var request = store.add(customerData);

request.onsuccess = function(e) {
console.log("Woot! Did it");
}
request.onerror = function(e) {
console.log("Error",e.target.error.name);
}
}
} catch (ex) { 
console.log("Message is: " +ex.message); 
}  
}

1 个答案:

答案 0 :(得分:0)

window.indexedDB = window.indexedDB || window.webkitIndexedDB || window.mozIndexedDB || window.OIndexedDB || window.msIndexedDB;
var myIDBTransaction = window.IDBTransaction || window.webkitIDBTransaction || { READ_WRITE: "readwrite" };
dbVersion = 1;
var db;
var dbreq;
var objectStore1;
var customerData =
{
ssn: "444", 
name: "Bill", 
email: "bill@company.com"
};

function create_openDB() 
{
    if (!window.indexedDB) 
    {
    alert("Does not support IndexedDB");
    } 
    else
    {
        alert("Supported. Go ahead");
    }
    alert('In DB function');
    try {
              dbreq = indexedDB.open("IndexDB", dbVersion);
              console.log(dbreq);
              dbreq.onsuccess = function (event) {
                  alert("Successful database creation");
             }
             dbreq.onerror = function (event) {
                 console.log("indexedD Error: " + event.message);
             }
              dbreq.onupgradeneeded = function(event) {
    db = event.target.result;
        objectStore1 = db.createObjectStore("customers", { keyPath: "ssn" });
        console.log(objectStore1);
         objectStore1.createIndex("name", "name", { unique: false });
         objectStore1.createIndex("email", "email", { unique: true });
    var dbIndex = objectStore1.index("name"); //works only if the specific index is present
                 console.log(dbIndex);

                 console.log("Object store created");

                 createDatabaseObjects(db);
                } 
        }catch (e) {
             console.log("Error: " + e.message);
         }
 }


function createDatabaseObjects(dbHandle) {
 try 
 {
 if ( dbHandle == null ) {
    console.log( "Can't create database objects; the database is not open." );
   } 
   else 
   {
  console.log("in else");
  objectStore1.transaction.oncomplete = function(e)
  {
         var trans = dbHandle.transaction(["customers"], "readwrite");
        console.log("after transaction");

        var customers1 = trans.objectStore("customers");
         var request = customers1.put(customerData);

        request.onsuccess = function(e) {
         dbHandle.objectStoreId = request.result;
         console.log(request.result);
         console.log("Woot! Did it");
        }
        request.onerror = function(e) {
            console.log("Error",e.target.error.name);
        }

    }
}

 } catch (ex) { 
    console.log("Message is: " +ex.message); 
}  
}