IndexedDb 版本更改交易是 运行

IndexedDb version change transaction is running

我是 JSON 和 indexeddb 的新手。我正在尝试创建一个新的 table 并向其添加数据,但我总是在 indexdb 的 chrome 控制台中收到此错误。我已经完成了 indexdb 的所有 Whosebug 内容,但我仍然无法取得进展。 如果我更改现有对象存储代码以替换其数据,它会起作用,但是当我尝试添加新对象存储并添加数据时,它会失败。 这是错误:

Uncaught DOMException: Failed to execute 'transaction' on 'IDBDatabase': A version change transaction is running. at populateGHGEF (https://localhost:44364/js/demo/sample.js:437:18) at IDBOpenDBRequest.upgradeNeededFunction (https://localhost:44364/js/demo/sample.js:420:17)

它发生在这里:

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

谁能告诉我我做错了什么。

这个错误的原因在行

function populateGHGEF(db, data) {
        var transaction = db.transaction(["gHGEF"], "readwrite");

您从 onupgradeneeded 处理程序中调用。

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

populateGHGEF(newVersion, ghgEFdata);

e.target.transaction.oncomplete = function() {
  populateGHGEF(newVersion, ghgEFdata);
};

但是,如果您只是等待插入数据直到适当的时间,也就是 IDBOpenRequest 成功完成时,您就可以完全避免这种情况。这是数据库被认为打开的时间。

作为一条非正式规则,您不应在 onupgradeneeded 处理程序中存储数据。该功能仅供程序员使用,因为有时升级数据库需要更改数据的存储方式。但是如果您不升级数据库(例如更改对象存储),那么您不应该在 onupgradeneeded 中进行插入。

改进:

  • 在onsuccess handler中做数据修改,就不用每次都升级版本修改数据了
  • 使用承诺来帮助完全分离调用