axemclion / jquery-indexeddb

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

Cannot update objects #38

Closed jtrishmawi closed 11 years ago

jtrishmawi commented 11 years ago

Hi all, I tried to update an object using the put method and it doesn't work.

Here is how I try to do it:

var database = $.indexedDB("db", {
        "schema": {
            "2": function(versionTransaction) {
                var customers = versionTransaction.createObjectStore("customers", {
                    "keyPath": "id",
                    "autoIncrement": true
                });
                customers.createIndex("id");
            }
        }
    });

var id = parseInt($(this).data('id')); //this value is taken from a link

var data = {  //this is an object that is fetched from a form 
        "name" : "123",
        "telephone" : "123",
        "mobile" : "123",
        "mobile2" : "123",
        "email" : "123",
        "id": id // this is the id and it is added manually 
};

database.objectStore("customers").put(data, data.id).then(function(result) {
        console.log(result);
    }, error);

I'm getting this error message:

"exception" main.js:288
{"message":"DataError: DOM IDBDatabase Exception 0","name":"DataError","code":0,"stack":"Error: The data provided does not meet requirements.\n    at http://localhost/js/vendor/db.js:128:36\n    at Object.<anonymous> (http://localhost/js/vendor/db.js:57:53)\n    at Function.b.extend.Deferred (http://localhost/js/vendor/jquery-1.9.1.min.js:3:9722)\n    at Object.wrap.request (http://localhost/js/vendor/db.js:55:15)\n    at Object.put (http://localhost/js/vendor/db.js:127:21)\n    at http://localhost/js/vendor/db.js:468:42\n    at onTransactionProgress (http://localhost/js/vendor/db.js:399:10)\n    at IDBTransaction.<anonymous> (http://localhost/js/vendor/db.js:461:9)\n    at IDBTransaction.<anonymous> (http://localhost/js/vendor/jquery-1.9.1.min.js:3:9221)\n    at c (http://localhost/js/vendor/jquery-1.9.1.min.js:3:7857)"} main.js:289
Transaction completed

Can you please tell me where i'm wrong?

axemclion commented 11 years ago

@maramtech You should not specify the key explicitly if you have already specified the keypath. Try doing

database.objectStore("customers").put(data)

jtrishmawi commented 11 years ago

Thank you so much... I really feel stupid now, i'w lost 2 3 hours on this :+1: Your plugin is simply wonderful and easy to use. I've done a complete crud app in a day or less.. Great job