IndexedDB在Firefox 29中不起作用,但是在Chrome中 [英] IndexedDB not working in Firefox 29, but in Chrome

查看:172
本文介绍了IndexedDB在Firefox 29中不起作用,但是在Chrome中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到问题,我使用控制台在Chrome IndexedDB上开发了50%的代码。不过要在Firefox上测试这个代码,我注意到这是行不通的。


$ b

异步函数不会调用JavaScript Firefox包含在HTML中:

 < script src =./ js / bdLocal.js>< / script> 

bdLocal.js:

  var db; 

函数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});
};


解决方案

您可能正在尝试使用一个异步功能同步。 Mozilla网站上的例子不幸在这一点上是非常错误的。不幸的是,HTML5Rocks上的几个例子也是如此。以下方法会在任何浏览器中导致很多问题:

  var unreliableGlobalDatabaseConnectionVariable; 
var request = indexedDB.open(...);
request.onsuccess = function(){
var reliableDatabaseConnectionVariable = request.result;
unreliableGlobalDatabaseConnectionVariable = reliableDatabaseConnectionVariable;
};

var transaction = unreliableGlobalDatabaseConnectionVariable.transaction(...);
//等等

indexedDB.open是一个异步函数。这意味着很多东西,其中两个很重要,在这里指出:


  1. 全局数据库变量将被定义,直到请求.onsuccess执行。因此,在这个时间点之前访问全局变量的任何尝试都将不起作用,因为此时该变量是未定义的。在同一个作用域中的request.onsuccess之后的任何代码都是同步的,因此也可能在之前。即使代码在更新的一行,仍然是

  2. 一旦request.onsuccess完成,全局数据库变量可能变为关闭,null ,或者未定义的,或者其他任何时候无用的。可能是1纳秒之后,可能是5毫秒之后,可能是一个小时之后,可能永远不会。以后不保证数据库连接保持打开状态。一般来说,一些使indexedDB在浏览器中工作的人的方式导致数据库连接在连接上没有打开实时事务后关闭一小段时间。

  3. ol>

    请试试以下方法:

      var openDatabaseRequest = indexedDB。开(名称,版本); 

    openDatabaseRequest.onsuccess = function(event){
    console.log('Connected');
    var db = openDatabaseRequest.result;

    //只能访问这个函数中的db变量
    //保证被定义并打开
    //作为函数的作用域(所有的语句) 。例如,
    //只在这个
    //函数中放入并打开游标。
    var transaction = db.transaction(...);
    var store = transaction.objectStore(...);
    var request = store.put(...);
    request.onsuccess = function(){
    console.log('put was successful');
    };
    };

    这个问题可能是重复的:



    这个问题实际上与indexedDB无关,但与在Javascript中使用异步代码。因此,它可能是有关使用XMLHttpRequest的数百个问题的重复。


    I have a problem I have developed 50% of the code on Chrome IndexedDB using your console. However to test this code on Firefox I've noticed that does not work.

    The asynchronous functions are not calling this JavaScript Firefox is included on a 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 are probably trying to use an asynchronous function synchronously. The examples on the Mozilla website are unfortunately very wrong on this point. Unfortunately, so are several of the examples on HTML5Rocks. The following approach will cause you a lot of problems in any browser:

    var unreliableGlobalDatabaseConnectionVariable;
    var request = indexedDB.open(...);
    request.onsuccess = function() {
      var reliableDatabaseConnectionVariable = request.result;
      unreliableGlobalDatabaseConnectionVariable = reliableDatabaseConnectionVariable;
    };
    
    var transaction = unreliableGlobalDatabaseConnectionVariable.transaction(...);
    // etc.
    

    indexedDB.open is an asynchronous function. This means many things, two of which are important to point out here:

    1. The global db variable will be undefined up until the point that request.onsuccess executes. Therefore, any attempt to access the global variable before this point in time will not work because at this point in time the variable is undefined. Any code that follows request.onsuccess in the same scope is synchronous, and therefore also potentially before. Even code that is on a new line, farther down, is still before.
    2. Once request.onsuccess finishes, then the global db variable may become closed, null, or undefined, or otherwise useless, at any point in time afterward. It could be 1 nanosecond later, it could be 5 milliseconds later, it could be an hour later, it could be never. There is no guarantee the database connection remains open afterward. In general, the way that some of the people that make indexedDB work inside the browser cause the database connection to be closed some small amount of time after there are no live transactions open on the connection.

    Try experimenting with the following approach instead:

    var openDatabaseRequest = indexedDB.open(name,version);
    
    openDatabaseRequest.onsuccess = function(event) {
      console.log('Connected');
      var db = openDatabaseRequest.result;
    
      // Only access the db variable within this function
      // where it is guaranteed to be defined and open 
      // for the scope (all statements inside) of this function. For example,
      // do puts and gets and open cursors only inside this 
      // function.
      var transaction = db.transaction(...);
      var store = transaction.objectStore(...);
      var request = store.put(...);
      request.onsuccess = function() {
        console.log('put was successful');
      };
    };
    

    This question is probably a duplicate of:

    This question is actually unrelated to indexedDB, but related to the use asynchronous code in Javascript. It is therefore probably a duplicate of the hundreds of questions about using XMLHttpRequest.

    这篇关于IndexedDB在Firefox 29中不起作用,但是在Chrome中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆