Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-core/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
IndexedDB不适用于Firefox 29,但适用于Chrome_Firefox_Indexeddb - Fatal编程技术网

IndexedDB不适用于Firefox 29,但适用于Chrome

IndexedDB不适用于Firefox 29,但适用于Chrome,firefox,indexeddb,Firefox,Indexeddb,我有一个问题,我已经用你的控制台在Chrome IndexedDB上开发了50%的代码。然而,要在Firefox上测试这段代码,我注意到它不起作用 异步函数不调用此JavaScript Firefox包含在HTML中: <script src="./js/bdLocal.js"></script> 您可能正在尝试同步使用异步函数。不幸的是,Mozilla网站上的例子在这一点上是非常错误的。不幸的是,HTML5上的几个例子也是如此。以下方法在任何浏览器中都会给您带来很多问

我有一个问题,我已经用你的控制台在Chrome IndexedDB上开发了50%的代码。然而,要在Firefox上测试这段代码,我注意到它不起作用

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

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

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

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

var transaction = unreliableGlobalDatabaseConnectionVariable.transaction(...);
// etc.
indexedDB.open是一个异步函数。这意味着很多事情,这里需要指出其中两件事:

  • 在执行request.onsuccess之前,全局db变量将是未定义的。因此,在此时间点之前访问全局变量的任何尝试都将不起作用,因为此时该变量尚未定义。同一作用域中request.onsuccess后面的任何代码都是同步的,因此也可能是同步的。即使是在新行、更远的地方的代码,仍然是以前的代码
  • 一旦request.onsuccess完成,全局db变量可能会在随后的任何时间点变为关闭、null或未定义,或以其他方式无效。可能是1纳秒后,可能是5毫秒后,可能是一小时后,可能永远不会。无法保证数据库连接在之后保持打开状态。一般来说,一些使用indexedDB的人在浏览器中工作的方式会导致数据库连接在连接上没有打开的实时事务后关闭一小段时间
  • 请尝试使用以下方法:

    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');
      };
    };
    
    这个问题可能与以下问题重复:


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

    我投反对票,因为下面的实质性答案已经两周了,您还没有回答。但是,您提出了一个新问题。不需要说:请对那些花时间帮助你的人保持礼貌,并且(至少)接受/支持他们。
    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');
      };
    };