提问人:Corbinian 提问时间:11/15/2023 最后编辑:Corbinian 更新时间:11/16/2023 访问量:63
声明 js 变量/对象时出错 (Uncaught DOMException)
Error when declaring a js variable/ Object (Uncaught DOMException)
问:
我的脚本遇到错误:
未捕获的 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");
}
答:
1赞
Cpavrai
11/15/2023
#1
在这里,if is used before ,即使你给它一个值,你的变量也没有被实例化。也许这是您的问题。onsuccess
onupgradeneeded
store
但是,您应该尝试使用,如果可能的话,为了更好地使用变量的作用域let
const
评论
0赞
Corbinian
11/15/2023
我现在尝试在.onupgradeneeded之外声明var store。它没有区别。
0赞
Rory McCrossan
11/15/2023
如果是这种情况,则错误将指向该行var marmaInfo = store.get(key);
0赞
Jeff B
11/16/2023
#2
看起来您需要等待返回的结果。此代码取自 mdn web 文档:getKey
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
0赞
Corbinian
11/16/2023
#3
绝对不是最好的解决方案。我经过反复试验到达那里,但它有效: (提示仍然被接受。
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;
}
评论
store.get(key)
console.log( marmaInfo );