IndexedDB 声明js变量/对象时出错(未捕获DOMException)

rqdpfwrv  于 11个月前  发布在  IndexedDB
关注(0)|答案(3)|浏览(177)

我的脚本运行到错误:
Uncaught DOMException:试图使用不可用或不再可用的对象
该脚本应检查,如果数据库已填充.如果没有填充数据库的初始值.之后显示的值在inputfields.错误发生在var data = marmaInfo.result

var indexedDB = window.indexedDB
var db;
    // Open (or create) the database
    var open = indexedDB.open("marmaDB", 1);
    console.log("open Db ...");

    // Create the schema
    console.log("creating schema ...");
    open.onupgradeneeded = function() {
        db = open.result;
        var store = db.createObjectStore("marmasStore", {keyPath: "id"});
    store.createIndex('sanskrit', 'sanskrit', { unique: false });
      };

    function getObjectStore(store_name, mode) {
        var tx = db.transaction(store_name, mode);
        return tx.objectStore(store_name);
    }

    open.onsuccess = function() {
        console.log("open DB DONE");
        // Start a new transaction
        db = open.result;
        store = getObjectStore("marmasStore", 'readwrite');
        //var tx = db.transaction("marmasStore", "readwrite");
        //var store = tx.objectStore("marmasStore");
        //var index = store.index("NameIndex");

        // Initial data
        const initialMarmaData = [
            { id: "ani_ll", sanskrit: "ani"},
        ];

        // Store values
        // initial DB filling 
        
        var key = $('#marmaID').val();
        console.log("key " + key );
        var marmaInfo = store.get(key);
        //marmaInfo.onsuccess = function(evt) {
        var data = marmaInfo.result  //###### ERROR at this line ######
        if (data.sanskrit == 0){
            console.log("record " + key + ":", record);
            console.log("initial filling DB ...");
            var store = getObjectStore("marmasStore", 'readwrite');
            initialMarmaData.forEach((marma) => {
                var request = store.put(marma);
                request.onsuccess = (event) => {
                    console.log(event.target.result + " initial filling DB DONE");
                };
            });
        }
        //}
        //fill the form with DB values
        console.log("filling form ...");
        document.getElementById('sanskrit').value = data.sanskrit;
        console.log("filling form DONE");
        
      }

字符串

6qftjkof

6qftjkof1#

在这里,如果onsuccessonupgradeneeded之前被调用,那么即使你给予变量store赋值,它也没有被示例化。这可能是你的例子中的一个问题。
但是,如果可能的话,您应该尝试使用letconst,以便更好地使用变量的作用域

lawou6xi

lawou6xi2#

看起来你需要等待getKey的结果返回。这段代码摘自mdn web docs:

let request = store.getKey(IDBKeyRange(yesterday, today));
  request.onsuccess = (event) => {
    let when = event.target.result;
    alert(`The 1st activity in last 24 hours was occurred at ${when}`);
  };

字符串
.onsuccess调用本质上是等待getKey函数返回,从而定义结果。

zaqlnxep

zaqlnxep3#

大多数绝对不是最好的解决方案。我带着试错到达那里,但它有效:(提示仍然很精确。)

var store;
var data;

字符串
[...]

open.onsuccess = function() {
console.log("open DB DONE");
// Start a new transaction
db = open.result;
store = getObjectStore("marmasStore", 'readwrite');
//var tx = db.transaction("marmasStore", "readwrite");
//var store = tx.objectStore("marmasStore");

// Initial data
const initialMarmaData = [
    { id: "ani_ll", sanskrit: "ani" },
];
// Store values
// initial DB filling 
var key = $('#marmaID').val();
console.log("key " + key );
store = getObjectStore("marmasStore", 'readwrite');

var marmaInfo = store.get(key);
marmaInfo.onsuccess = function(evt) {
    console.log(key + " FOUND");
    var record = evt.target.result;
    console.log("record:", record);
    if (typeof record == 'undefined') {
        displayActionFailure("No matching record found");
        return;
    }
    // Update the data
    //new values
    console.log("updating " + key + " ...");
    data = marmaInfo.result
    if (data.sanskrit == 0 ){
        console.log("record " + key + ":", record);
        console.log("initial filling DB ...");
        initialMarmaData.forEach((marma) => {
        var request = store.put(marma);
        request.onsuccess = (event) => {
            console.log(event.target.result + " initial filling DB DONE");
        };
    });
    }
    document.getElementById('sanskrit').value = data.localisation;
}

相关问题