我正在尝试编写一个简单的 JavaScript 应用程序,以从 Web 表单向 indexedDB 添加条目。由于某种原因,添加请求显示为成功,但即使刷新数据库,我也无法在 chrome 开发人员窗口中看到任何内容。这是我正在编写的 JavaScript。
$(document).ready(function() {
$("#addcsub").click(function(e){
e.preventDefault();
//add the contact
addContact($("#clast").val(), $("#cfirst").val(), $("#cphone").val(), $("#cemail").val());
});
});
document.addEventListener("DOMContentLoaded", function() {
if(!"indexedDB" in window){
return;
}
var openRequest = indexedDB.open("theDatabase",1);
openRequest.onupgradeneeded = function(e) {
var thisDB = e.target.result;
if(!thisDB.objectStoreNames.contains("contacts")) {
var objectStore = thisDB.createObjectStore("contacts", {keyPath:"contactid",autoIncrement: true});
objectStore.createIndex("contactid","contactid",{unique:true});
objectStore.createIndex("lastname","lastname", {unique:false});
objectStore.createIndex("firstname","firstname", {unique:false});
objectStore.createIndex("phone","phone", {unique:false});
objectStore.createIndex("email","email", {unique:false});
}
}
openRequest.onsuccess = function(e) {
db = e.target.result;
}
openRequest.onerror = function(e) {
console.log("Open Request Error");
}
},false);
function addContact(last,first,phone,email){
var transaction = db.transaction(["contacts"],"readwrite");
var store = transaction.objectStore("contacts");
var tc = {
lastname:last,
firstname:first,
phone:phone,
email:email
}
var request = store.add(tc);
request.onerror = function(e){
console.error(request.error);
}
request.onsuccess = function(e){ //this runs when I hit submit
console.log("Add Successful"); //this appears in the console
}
}
更新:我做了一些研究,发现我的添加事务正在中止,并出现 DOMException 代码 22 的错误。
更新 2:由于某种原因,它可以在其他计算机上运行,但不能在我的笔记本电脑上运行。
最佳答案
我在使用 indexedDb 时遇到了 devtools gui 的问题。您可以尝试通过控制台拉出记录,看看它是否存在,但在过去,我必须关闭开发工具并再次打开它才能刷新。
关于javascript - IndexedDB 添加条目显示成功,但未显示在 chrome 开发工具中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47521254/