我的脚本运行到错误:
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");
}
字符串
3条答案
按热度按时间6qftjkof1#
在这里,如果
onsuccess
在onupgradeneeded
之前被调用,那么即使你给予变量store
赋值,它也没有被示例化。这可能是你的例子中的一个问题。但是,如果可能的话,您应该尝试使用
let
和const
,以便更好地使用变量的作用域lawou6xi2#
看起来你需要等待
getKey
的结果返回。这段代码摘自mdn web docs:字符串
.onsuccess
调用本质上是等待getKey
函数返回,从而定义结果。zaqlnxep3#
大多数绝对不是最好的解决方案。我带着试错到达那里,但它有效:(提示仍然很精确。)
字符串
[...]
型