axemclion / jquery-indexeddb

An IndexedDB Plugin for Jquery.
Other
195 stars 71 forks source link

Add data or get data correction #33

Closed EduAir closed 11 years ago

EduAir commented 11 years ago

Hi to all I have some problem with jquery-indexeddb.It is a jquery pluggins.

I just want to get data or add data to the local database.

THis is the code that i use to create de database

var dbName = 'my_db';

if(window.indexedDB){

 var dbOpenPromise = $.indexedDB(dbName, {
    "version":1,
    "upgrade" :function(transaction){
         transaction.createObjectStore("article", {
            "keyPath": "page_id","autoIncrement": false
        }).createIndex("title");

         transaction.createObjectStore("all_article", {
           "keyPath": "taleau"
        });
    }
 });
}

And this is the code that i use to add data

var request = window.indexedDB.open(dbName);

var objectStore = $.indexedDB(dbName).objectStore("article");

var promise = objectStore.add(page_aff,page_id);

promise.done(function(result, event){ console.log(result); });

                                        promise.fail(function(error,event){

  console.log(event);/*this line shows the error*/
                                        });

I have this error to the log

DOMException {message: "DataError: DOM IDBDatabase Exception 0", name: "DataError", code: 0, stack: "Error: The data provided does not meet requirement…calhost/begoo/assets/js/jquery/jquery.js:2:15502)", INDEX_SIZE_ERR: 1…}

code: 0 message: "DataError: DOM IDBDatabase Exception 0" name: "DataError" stack: "Error: The data provided does not meet requirements.↵ at http://localhost/begoo/assets/js/jquery/jquery.indexeddb.js:128:36↵ at Object. (http://localhost/begoo/assets/js/jquery/jquery.indexeddb.js:57:53)↵ at Function.f.extend.Deferred (http://localhost/begoo/assets/js/jquery/jquery.js:2:16638)↵ at Object.wrap.request (http://localhost/begoo/assets/js/jquery/jquery.indexeddb.js:55:15)↵ at Object.add (http://localhost/begoo/assets/js/jquery/jquery.indexeddb.js:127:21)↵ at http://localhost/begoo/assets/js/jquery/jquery.indexeddb.js:579:42↵ at onTransactionProgress (http://localhost/begoo/assets/js/jquery/jquery.indexeddb.js:509:10)↵ at IDBTransaction. (http://localhost/begoo/assets/js/jquery/jquery.indexeddb.js:572:9)↵ at o (http://localhost/begoo/assets/js/jquery/jquery.js:2:14733)↵ at Object.p.fireWith (http://localhost/begoo/assets/js/jquery/jquery.js:2:15502)"

I'm working on Chrome and i see that my database is created but no data added.I think that i follow the tutorial.

axemclion commented 11 years ago

What is the value of page_aff ? Since the object store has a keypath defined, you dont give it a key, the page_id should be a property of the page_af object.

EduAir commented 11 years ago

Tank's for your reaction.

the value of page_aff is a json object whose come from a wikipedia api that i use to retrieve articles. Please sorry for my english

EduAir commented 11 years ago

Thank's it is work fine.I followed your answer. But in index i want to save the title of an article that i can fin in json object like this:page_aff.query.pages[page_id].title wiht jquery.The index is not automatically populated.In don't know if it is about the complexity of the json objet whose have many tree.

axemclion commented 11 years ago

In that case, you can either omit the keyPath and instead specify the key, or ensure that a page_id exists in the keypath.