2013-07-01 192 views
0

錯誤是從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(); 


}); 
+0

$('date').val()的值是多少? –

+0

@ Kristof Degrave nw我添加了console.log(「輸入的日期是」+日期);但我發現沒有值存儲在日期變量,所以我認爲這是根問題...如何解決這個問題??? plz help –

回答

0

檢索您的字段的值可以通過ID來完成:(IDS與工作時,不要忘了#)

var date = $('#date').val(); 

這可能會導致問題,則使用date作爲對象存儲的關鍵字,所以在這種情況下,它需要是唯一的,並且是以下類型之一:DOMstring,array,日期數。含義未定義將不被接受。

+0

@ Kristof Degrave ..ok現在我在控制檯中獲取日期。日誌輸入日期是:2013-03-06 ...我應該如何修改獲取項目中的語句(日期)我仍然收到錯誤:DOM IDBdatabase異常8爲行var index = store.index(date); –

+0

我認爲你必須將商店名稱傳遞給交易方法。 –

+0

@ Kristof Degrave我沒有明白你的意思......我做了一個全局變量var storeName =「diab」,並以這種方式傳遞它.. var store = getObjectStore(storeName,'readonly');錯誤保持不變...幫助... –