Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/453.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
Javascript IndexedDb版本更改事务正在运行_Javascript_Indexeddb - Fatal编程技术网

Javascript IndexedDb版本更改事务正在运行

Javascript IndexedDb版本更改事务正在运行,javascript,indexeddb,Javascript,Indexeddb,我不熟悉JSON和indexeddb。我试图创建一个新表并向其中添加数据,但我总是在用于indexdb的chrome控制台中遇到此错误。我已经完成了indexdb的所有stackoverflow工作,但仍然无法取得进展。 如果我更改现有对象存储代码以替换其数据,它会工作,但当我尝试添加新对象存储并添加数据时,它会失败。 以下是错误: 未捕获的DomeException:未能在上执行“事务” “IDBDatabase”:正在运行版本更改事务。 在populateGHGEF()处 在IDBDOpen

我不熟悉JSON和indexeddb。我试图创建一个新表并向其中添加数据,但我总是在用于indexdb的chrome控制台中遇到此错误。我已经完成了indexdb的所有stackoverflow工作,但仍然无法取得进展。 如果我更改现有对象存储代码以替换其数据,它会工作,但当我尝试添加新对象存储并添加数据时,它会失败。 以下是错误:

未捕获的DomeException:未能在上执行“事务” “IDBDatabase”:正在运行版本更改事务。 在populateGHGEF()处 在IDBDOpenDBRequest.UpgradeNeedFunction()处

发生在这里:

function populateGHGEF(db, data) {
        var transaction = db.transaction(["gHGEF"], "readwrite");
这是我的密码:

var tacInputsDb = (function setupIndexDb() {
    var DB_NAME = "TacInputsDb";
    var DB_VERSION = "1";
    var db;

    function init(callback) {
        window.indexedDB = window.indexedDB || window.mozIndexedDB || window.webkitIndexedDB ||
            window.msIndexedDB;
        window.IDBTransaction = window.IDBTransaction || window.webkitIDBTransaction ||
            window.msIDBTransaction;

        //always starting fresh...for now.
        //console.log("Deleting current database.");
        //indexedDB.deleteDatabase("InitDB");

        if (!window.indexedDB) {
            window.alert("Your browser doesn't support a stable version of IndexedDB.");
        }

        //console.group("Index DB");

        var openRequest = window.indexedDB.open(DB_NAME, DB_VERSION);

        openRequest.onupgradeneeded = function upgradeNeededFunction(e) {
            //console.log("If upgrade needed");
            var newVersion = e.target.result;


            if (!newVersion.objectStoreNames.contains("GHGEF")) {
                newVersion.createObjectStore("GHGEF", {
                    keyPath: "FuelType"
                    //autoIncrement: true
                }

                );

                var ghgEFdata = [
                    {
                        "FuelType": "Biodiesel",
                        "Energy Density (MJ/unit)": " 126.13 ",
                        "Carbon Intensity \r\n(gCO2/MJ)": " 34.34 ",
                        "Diesel Gallon Equivalent (DGE)": "0.00",
                        "Gasoline Gallon Equivalent (GGE)": "0.00",
                        "Diesel Gallon Equivalent Emission Factor (gCO2e/DGE)": "",
                        "Gasoline Gallon Equivalent Emission Factor (gCO2e/GGE)": "",
                        "EER Values Relative to Diesel": "1.0",
                        "EER Values Relative to Gasoline": " 1.1 "
                    },
                    {
                        "FuelType": "CNG",
                        "Energy Density (MJ/unit)": " 0.98 ",
                        "Carbon Intensity \r\n(gCO2/MJ)": " 79.21 ",
                        "Diesel Gallon Equivalent (DGE)": "0.00",
                        "Gasoline Gallon Equivalent (GGE)": "0.00",
                        "Diesel Gallon Equivalent Emission Factor (gCO2e/DGE)": "",
                        "Gasoline Gallon Equivalent Emission Factor (gCO2e/GGE)": "",
                        "EER Values Relative to Diesel": "0.9",
                        "EER Values Relative to Gasoline": " 1.0 "
                    },
                    {
                        "FuelType": "Diesel",
                        "Energy Density (MJ/unit)": " 134.47 ",
                        "Carbon Intensity \r\n(gCO2/MJ)": " 100.45 ",
                        "Diesel Gallon Equivalent (DGE)": "0.00",
                        "Gasoline Gallon Equivalent (GGE)": "0.00",
                        "Diesel Gallon Equivalent Emission Factor (gCO2e/DGE)": "",
                        "Gasoline Gallon Equivalent Emission Factor (gCO2e/GGE)": "",
                        "EER Values Relative to Diesel": "1.0",
                        "EER Values Relative to Gasoline": " 1.1 "
                    },
                    {
                        "FuelType": "Electric",
                        "Energy Density (MJ/unit)": " 3.60 ",
                        "Carbon Intensity \r\n(gCO2/MJ)": " 81.49 ",
                        "Diesel Gallon Equivalent (DGE)": "0.00",
                        "Gasoline Gallon Equivalent (GGE)": "0.00",
                        "Diesel Gallon Equivalent Emission Factor (gCO2e/DGE)": "",
                        "Gasoline Gallon Equivalent Emission Factor (gCO2e/GGE)": "",
                        "EER Values Relative to Diesel": "5.0",
                        "EER Values Relative to Gasoline": " 3.4 "
                    },
                    {
                        "FuelType": "Gasoline",
                        "Energy Density (MJ/unit)": " 115.83 ",
                        "Carbon Intensity \r\n(gCO2/MJ)": " 99.44 ",
                        "Diesel Gallon Equivalent (DGE)": "0.00",
                        "Gasoline Gallon Equivalent (GGE)": "0.00",
                        "Diesel Gallon Equivalent Emission Factor (gCO2e/DGE)": "",
                        "Gasoline Gallon Equivalent Emission Factor (gCO2e/GGE)": "",
                        "EER Values Relative to Diesel": "1.0",
                        "EER Values Relative to Gasoline": " 1.0 "
                    },
                    {
                        "FuelType": "Hydrogen Fuel Cell",
                        "Energy Density (MJ/unit)": " 120.00 ",
                        "Carbon Intensity \r\n(gCO2/MJ)": " 111.61 ",
                        "Diesel Gallon Equivalent (DGE)": "0.00",
                        "Gasoline Gallon Equivalent (GGE)": "0.00",
                        "Diesel Gallon Equivalent Emission Factor (gCO2e/DGE)": "",
                        "Gasoline Gallon Equivalent Emission Factor (gCO2e/GGE)": "",
                        "EER Values Relative to Diesel": "1.9",
                        "EER Values Relative to Gasoline": " 2.5 "
                    },
                    {
                        "FuelType": "LNG",
                        "Energy Density (MJ/unit)": " 78.83 ",
                        "Carbon Intensity \r\n(gCO2/MJ)": " 97.77 ",
                        "Diesel Gallon Equivalent (DGE)": "0.00",
                        "Gasoline Gallon Equivalent (GGE)": "0.00",
                        "Diesel Gallon Equivalent Emission Factor (gCO2e/DGE)": "",
                        "Gasoline Gallon Equivalent Emission Factor (gCO2e/GGE)": "",
                        "EER Values Relative to Diesel": "0.9",
                        "EER Values Relative to Gasoline": " 1.0 "
                    },
                    {
                        "FuelType": "Renewable Diesel",
                        "Energy Density (MJ/unit)": " 129.65 ",
                        "Carbon Intensity \r\n(gCO2/MJ)": " 30.40 ",
                        "Diesel Gallon Equivalent (DGE)": "0.00",
                        "Gasoline Gallon Equivalent (GGE)": "0.00",
                        "Diesel Gallon Equivalent Emission Factor (gCO2e/DGE)": "",
                        "Gasoline Gallon Equivalent Emission Factor (gCO2e/GGE)": "",
                        "EER Values Relative to Diesel": "1.0",
                        "EER Values Relative to Gasoline": " 1.1 "
                    },
                    {
                        "FuelType": "Renewable Natural Gas",
                        "Energy Density (MJ/unit)": " 0.98 ",
                        "Carbon Intensity \r\n(gCO2/MJ)": " 43.84 ",
                        "Diesel Gallon Equivalent (DGE)": "0.00",
                        "Gasoline Gallon Equivalent (GGE)": "0.00",
                        "Diesel Gallon Equivalent Emission Factor (gCO2e/DGE)": "",
                        "Gasoline Gallon Equivalent Emission Factor (gCO2e/GGE)": "",
                        "EER Values Relative to Diesel": "0.9",
                        "EER Values Relative to Gasoline": " 1.0 "
                    }
                ];
                //populateGHGEF(newVersion);
                populateGHGEF(newVersion, ghgEFdata);
            }
        };

        //openRequest.onerror = openRequest.onblocked = ////console.log("Blocked");
        openRequest.onerror = openRequest.onblocked = function (e) {
            ////console.log("InitDB Blocked.");
        };

        openRequest.onsuccess = function (e) {
            db = e.target.result;
            callback("Tac Inputs Db setup Complete. Database is open.");
        };
    }//END init()

    function populateGHGEF(db, data) {
        var transaction = db.transaction(["gHGEF"], "readwrite");
        console.log("created transaction for ghgef");

        //Do something when all the data is added to the database.
        transaction.oncomplete = function (event) {
            console.log("All done!");
        };

        transaction.onerror = function (event) {
            console.log("Don't forget to handle errors!");
        };

        var objectStore = db.objectStore("gHGEF");
        console.log("accessing object store");

        data.forEach(function (GHGEF) {
            var request = objectStore.add(GHGEF);
            request.onsuccess = function (event) {
                ////console.log("ghgRegion Item Successfully Added.");
            };
        });
    }


        });
    }


    return {
        init: init,

    }
})()
下面是我在另一页上的称呼:

(function ($) {
    $(document).ready(function () {
        var p1 = Promise.resolve();
        p1 = p1.then(_ => new Promise(resolve =>
            tacInputsDb.init(function (msg) {
                console.log(msg);
                });
            })
        ));
        });

有人能告诉我我做错了什么吗

此错误的原因在以下行中

函数populateHGEF(数据库,数据){
var transaction=db.transaction([“gHGEF”],“readwrite”);
从OnUpgradeRequired处理程序中调用的

作为快速修复,如果您坚持以这种方式编写代码,您可以等待versionchange事务完成,然后通过更改行开始第二个事务

populateGHGEF(新版本,ghgEFdata);

e.target.transaction.oncomplete=function(){
PopulateHGEF(新版本,ghgEFdata);
};
但是,如果您只是等待插入数据,直到适当的时间,也就是IDBOpenRequest成功完成时,才认为数据库是打开的,则可以完全避免这种情况

作为一项非正式规则,您不应在OnUpgradeRequired handler上存储来自内部的数据。该功能仅对程序员可用,因为有时升级数据库需要更改数据的存储方式。但如果您不升级数据库(例如,更改对象存储),则不应在ONUPGRADE中执行插入操作

改进:

  • 在onsuccess处理程序中进行数据更改,则不必每次都升级版本来更改数据
  • 使用承诺帮助完全分离通话

我不得不修改这个
e.target.transaction.oncomplete=function(){populateGHGEF(新版本,ghgEFdata);}
那么您可能想更改您对此的答案?您能帮我改进代码吗?您建议我在哪里以及如何调用数据填充方法?我是否应该将调用完全分离为1。调用Db schema setup 2。调用数据设置?这是我尝试使用的前一个开发人员的代码。我甚至不知道是什么indexeddb一直到现在都是。