0
votes

The error is of failed retrieval of objects from the db. The exception is Exception 0 i.e. data provided to operation does not meet requirements. I want to retrieve the object values of date,pre,post depending upon the date provided in the input field. Also one error is of report is undefined. plz help..

<!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();


});
1
what is the value of $('date').val(); - Kristof Degrave
@ Kristof Degrave nw i added console.log("Date entered is"+ date); but i found that no value was stored in date variable,so i think this is the problem root...how to solve the issue ???..plz help - Abhishek Jain

1 Answers

0
votes

retrieving the values of your field can be done by id: (don't forget the # when working with ids)

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

This will probably cause the issue, you are using date as key for the object store, so in that case it needs to be unique and of one of the following types: DOMstring, array, number of date. Meaning undefined will not be accepted.