エラーは、データベースからのオブジェクトの取得に失敗したことです。例外は例外 0 です。つまり、操作に提供されたデータが要件を満たしていません。入力フィールドに指定された日付に応じて、date、pre、post のオブジェクト値を取得したいと考えています。また、レポートが定義されていないエラーが 1 つあります。助けてください..
<!DOCTYPE html>
<html manifest="manifest.webapp" lang="en">
<head>
<meta charset="utf-8">
<title>Diab</title>
<!link rel="stylesheet" href="diab.css">
<script src="http://ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js"></script>
</head>
<body>
<input type="date" id="date" >Date</input>
<input type="number" id="pre">Pre</input>
<input type="number" id="post">Post</input>
<button id="add">Add</button>
<button id="show">Show</button>
<script type="text/javascript" src="diab3.js"></script>
</body>
</html>
$(document).ready(function()
{
var db;
var openDb=function()
{
var request=indexedDB.open("diabetore2",2);
request.onsuccess = function()
{
console.log("DB created succcessfully");
db = request.result;
console.log("openDB done!!");
return db;
};
request.onerror=function(){
alert("could not open db");
};
request.onupgradeneeded = function()
{
var db= request.onsuccess();
console.log("openDB.onupgradeneeded function");
var store = db.createObjectStore("diab", {keyPath: 'date'});
var dateIndex = store.createIndex("date", "date",{unique: true});
// Populate with initial data.
store.put({date: "june 1 2013",pre:70,post:70});
store.put({date: "june 2 2013",pre:71,post:87});
store.put({date: "june 3 2013",pre:72,post: 76});
store.put({date: "june 8 2013",pre:73,post:75});
};
};
function getObjectStore(store_name,mode)
{
var tx=db.transaction(store_name,mode);
return tx.objectStore(store_name);
}
function addItems(date,pre,post)
{
console.log("addition to db started");
var obj={date:date,pre:pre,post:post};
var store=getObjectStore("diab",'readwrite');
var req;
try
{
req=store.add(obj);
}catch(e)
{
if(e.name=='DataCloneError')
alert("This engine doesn't know how to clone");
throw(e);
}
req.onsuccess=function(evt)
{
console.log("****Insertion in DB successful!!****");
};
req.onerror=function(evt)
{
console.log("Could not insert into DB");
};
}
function getItems(date)
{ var gdate=date;
console.log("retrieval started from db");
var store=getObjectStore("diab","readonly");
var index=store.index("date");
var request=index.get(gdate);
request.onsuccess=function()
{
var matching=request.result;
if(matching !== undefined)
{
//report(matching.pre,matching.post);
alert(matching.pre+ " , "+matching.post);
}else
console.log("match not found");
//report (null);
};
}
$("#add").click(function(){
console.log("addEventListeners called...");
console.log("add...");
var date=document.getElementById('date').value;
var pre=document.getElementById('pre').value;
var post=document.getElementById('post').value;
if(!date)
{
alert("required field missing..");
return;
}
addItems(date,pre,post);
});
$("#show").click(function(){
console.log("eventlistner called for retrieval..");
console.log("retrieve");
var date=$('date').val();
/*if(!date)
{
alert("required field missing..");
return;
}*/
getItems(date);
});
openDb();
//addEventListners();
//here();
});