Error: "Failed to read the 'result' property from 'IDBRequest': The request has not finished" when try to modify in a loop

Error: "Failed to read the 'result' property from 'IDBRequest': The request has not finished" when try to modify in a loop

我正在使用 IndexedDB,当我尝试 modify/edit 多个具有相同文本的值并使用 "FOR" 循环时,它显示:"Failed to read the 'result' property from 'IDBRequest': The request has not finished".

当我尝试只使用一个没有循环的值时,一切正常。这是我的实际功能:

function modificarModelos(){
      var active = dataBase.result;
      var data = active.transaction(["modelos"], "readwrite");
      var object = data.objectStore("modelos");
      var request;
      for(var key in window.idModelosMarca){
        request = object.get(window.idModelosMarca[key]);
        request.onsuccess = function() {
          var data = request.result;   //---> THE ERROR APPEARS IN THIS LINE
          data.idmarca = document.querySelector("#NuevoNombreMarca").value
          var requestActualizado = object.put(data);
          requestActualizado.onsuccess = function() {
            console.log("Success");
          };
          requestActualizado.onerror = function (e) {
            alert(request.error.name + '\n\n' + request.error.message);
          };
        };
      }
    }

我该怎么做才能让它发挥作用?

谢谢。

看来您是异步编程的新手。我认为解决此问题的最简单、最短且最简单的方法是 避免在循环内定义函数

我解决了通过另一个内部有循环的函数调用该函数的问题:

function aux(){
   ....
   for(i=0;i<someCounter;i++){
      modificarModelos(somevalue);       
   }       
}