错误是从db中检索对象失败。例外0是例外,即提供给操作的数据不符合要求。我想根据输入字段中提供的日期检索date,pre,post的对象值。还有一个错误是报告未定义。请帮助..
<!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();
});
答案 0 :(得分:0)
检索字段的值可以通过id来完成:(使用id时不要忘记#)
var date = $('#date').val();
这可能会导致问题,您使用date作为对象存储的键,因此在这种情况下,它需要是唯一的并且具有以下类型之一:DOMstring,array,date of date。未定义的含义将不被接受。