錯誤是從db中檢索對象失敗。異常0例外,即提供給操作的數據不符合要求。我想根據輸入字段中提供的日期來檢索日期,前置,後置的對象值。還有一個報告錯誤是未定義的。 PLZ幫助..索引DB檢索失敗
<!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();
});
$('date').val()的值是多少? –
@ Kristof Degrave nw我添加了console.log(「輸入的日期是」+日期);但我發現沒有值存儲在日期變量,所以我認爲這是根問題...如何解決這個問題??? plz help –