-1
WebフォームからindexedDBにエントリを追加するための簡単なJavaScriptアプリケーションを作成しようとしています。何らかの理由で追加リクエストが成功したと表示されますが、データベースを更新してもChrome開発者ウィンドウに何も表示されません。ここに私が書いているJavaScriptがあります。IndexedDB addエントリが正常に表示されますが、chrome devツールに表示されません。
$(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がコードのエラーでアップデート2 22
中止されたことに気づい:何らかの理由で、それは他のコンピュータ上で動作し、だけではない私ラップトップ。
ありがとうございます、コンソールを閉じて再オープンしましたが、何も変わっていないようです。コンソールからレコードを引き出す最良の方法は何だと思いますか? –
ストアから取得するには、[docs](https://developer.mozilla.org/en-US/docs/Web/API/IDBObjectStore/get)を参照してください。 私はネイティブ構文以外の何かを使用することをお勧めします。私は岩石のラッパーである[dexie.js](http://dexie.org/)を使用することをお勧めします。 – Malisbad
正しい答えとしてあなたの印をつけて、私は自分の問題を解決しましたが、それは自分のコードではなく自分のコンピュータ自体の問題だと思います。 –