提问人:Corbinian 提问时间:11/15/2023 最后编辑:Corbinian 更新时间:11/15/2023 访问量:33
未能在全局范围内声明 Var
Failing to declare Var in global scope
问:
我想更改 indexedDB 的条目。因此,在输入字段中具有值并将它们提交到现有的数据库条目,然后在再次编辑它们之前检索它们。
现在脚本无法正常工作,因为: 未捕获的 TypeError:db 未定义
通过按下按钮,一个函数被调用,而该函数本身调用一个函数。
Buttonpressfunction --> “updateMarma” --> 使用 “getObjectStore” -->这需要声明 “db” 变量。$('#edit-button').click
起初我不小心把所有东西都放在“open.onsuccess = function() {”中,db变量是可访问的。但要使它发挥作用,这些功能需要由它们自己完成。但是现在我无法弄清楚如何/在哪里声明 db。
我也尝试过内部,但这仍然没有使它成为全球性的。
我也尝试过IEF。
我在文档中找不到任何其他提示,帮助会很多。open.onupgradeneeded = function() {
db = open.result;
(function() {
var indexedDB = window.indexedDB || window.mozIndexedDB || window.webkitIndexedDB || window.msIndexedDB || window.shimIndexedDB;
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() {
var db = open.result;
var store = db.createObjectStore("marmasStore", {keyPath: "id"});
var index = store.createIndex("NameIndex", ["name.last", "name.first"]);
store.createIndex('sanskrit', 'sanskrit', { unique: false });
store.createIndex('name', 'name', { unique: false });
store.createIndex('marmaGrp', 'marmaGrp', { unique: false });
store.createIndex('location', 'location', { unique: false });
store.createIndex('localisation', 'localisation', { unique: false });
store.createIndex('awareness', 'awareness', { unique: false });
store.createIndex('frequency', 'frequency', { 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
var db = open.result;
var tx = db.transaction("marmasStore", "readwrite");
var store = tx.objectStore("marmasStore");
var index = store.index("NameIndex");
// Add some data
const initialMarmaData = [
{ id: "ani_ll", sanskrit: "ani", name: "Ani", marmaGrp: "snayu", location: "linker Oberschenkel", localisation: 0, awareness: 0, frequency: 0 },
{ id: "ani_rl", sanskrit: "ani", name: "Ani", marmaGrp: "snayu", location: "rechter Oberschenkel", localisation: 0, awareness: 0, frequency: 0 },
{ id: "ani_la", sanskrit: "ani", name: "Ani", marmaGrp: "snayu", location: "linker Arm", localisation: 0, awareness: 0, frequency: 0 },
{ id: "ani_ra", sanskrit: "ani", name: "Ani", marmaGrp: "snayu", location: "rechter Arm", localisation: 0, awareness: 0, frequency: 0 },
];
// Store values in the newly created objectStore.
// put overwrites date in DB
// add can only be done once , second time script just stops.
console.log("initial filling DB ...");
var store = getObjectStore("marmasStore", 'readwrite');
initialMarmaData.forEach((marma) => {
var request = store.put(marma); // maybe change to add and programm errorhandler
request.onsuccess = (event) => {
console.log(event.target.result + " initial filling DB DONE");
};
});
// Close the db when the transaction is done
tx.oncomplete = function() {
db.close();
console.log("closing DB DONE");
};
}
// Query the data
function updateMarma(key) {
console.log("updatePublication:", arguments);
console.log("key:", key);
if (typeof store == 'undefined')
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;
}
console.log("Name for id is + ${marmaInfo.result.name}");
alert("ID " + marmaInfo.result.id + " sanskrit " + marmaInfo.result.sanskrit);
};
}
$('#edit-button').click(function(evt) {
console.log("edit ...");
var key = $('#marmaID').val();
var new_bodylocation = $('#bodylocation').val();
var new_awareness = $('#awareness').val();
if (key != '') {
console.log("editing " + key);
updateMarma(key);
} else {
console.log("no key provided");
displayActionFailure("Invalid key")
return;
}
});
})();
答: 暂无答案
评论