IndexedDB not working in Firefox 29, but in Chrome -
i have problem have developed 50% of code on chrome indexeddb using console. test code on firefox i've noticed not work.
the asynchronous functions not calling javascript firefox included on html:
<script src="./js/bdlocal.js"></script> bdlocal.js:
var db; function opendb() { console.log("opendb ") ; var request = indexeddb.open("dblocal",1); //db = request.result; request.onerror = function(event) { console.log("database error: " + event.target.errorcode); }; request.onsuccess = function(event) { console.log("database onsuccess: " ); db = request.result; }; request.onupgradeneeded = function(event) { console.log("onupgradeneeded"); var db = event.target.result; var objectstore = db.createobjectstore("customers", { keypath: "ssn" }); objectstore.createindex("name", "name", { unique: true }); objectstore.createindex("email", "email", { unique: true }); objectstore.createindex("matricula", "matricula", { unique: false }); }; }
you trying utilize asynchronous function synchronously. examples on mozilla website unfortunately wrong on point. unfortunately, several of examples on html5rocks. next approach cause lot of problems in browser:
var unreliableglobaldatabaseconnectionvariable; var request = indexeddb.open(...); request.onsuccess = function() { var reliabledatabaseconnectionvariable = request.result; unreliableglobaldatabaseconnectionvariable = reliabledatabaseconnectionvariable; }; var transaction = unreliableglobaldatabaseconnectionvariable.transaction(...); // etc. indexeddb.open asynchronous function. means many things, 2 of of import point out here:
the global db variable undefined until point request.onsuccess executes. therefore, effort access global variable before point in time not work because @ point in time variable undefined. code follows request.onsuccess in same scope synchronous, , hence potentially before. code on new line, farther down, still before. once request.onsuccess finishes, global db variable may become closed, null, or undefined, or otherwise useless, @ point in time afterward. 1 nanosecond later, 5 milliseconds later, hr later, never. there no guarantee database connection remains open afterward. in general, way of people create indexeddb work within browser cause database connection closed little amount of time after there no live transactions open on connection.try experimenting next approach instead:
var opendatabaserequest = indexeddb.open(name,version); opendatabaserequest.onsuccess = function(event) { console.log('connected'); var db = opendatabaserequest.result; // access db variable within function // guaranteed defined , open // scope (all statements inside) of function. example, // puts , gets , open cursors within // function. var transaction = db.transaction(...); var store = transaction.objectstore(...); var request = store.put(...); request.onsuccess = function() { console.log('put successful'); }; }; this question duplicate of:
uncaught typeerror: cannot read property 'transaction' of null indexeddb why db.transaction not working indexeddb? is bad open several database connections in indexeddb? indexeddb onupgradeneeded not called why onupgradeneeded callback never called when connecting indexeddb? why onsuccess/onerror callbacks not called when using indexeddb? why onsuccess called before onupgradeneeded when connecting indexeddb?this question unrelated indexeddb, related utilize asynchronous code in javascript. hence duplicate of hundreds of questions using xmlhttprequest.
firefox indexeddb
No comments:
Post a Comment