IndexedDB and Javascript: JSON and objects misunderstanding -
i'm trying obtain information json file download client through ajax , i'm getting different results depending on json format , don't know how fix 1 problem.
first case:
the json files looks like:
[{"name": "nick", "age": 28}, {"name": "katie", "age": 32}]
my ajax .done method looks like:
.done( function(data) { addobjectsdb (data, "people"); })
this method calls second 1 iterates through data , stored correctly each object indexeddb.
second case: have json file different format:
[ { "husband": { "name": "jhon", "age": 23 }, "wife": { "name": "marie", "age": 24 } } ]
now .done() ajax method iterates through data , add each person, husband or wife array sent db same method first case:
.done( function(data) { var people = []; $(data).each(function (key, value){ people.push(value.husband); people.push(value.wife); }); addobjectsdb (people, "people"); })
in case insertion database fails, if example, instead of adding value.husband people array add value people array insertion works, need each person stored separated in db.
the addobjectsdb method is:
function addobjectsdb (data, collection) { var objectstore = db.transaction(collection, "readwrite").objectstore(collection); $.each (data, function (key, value) { var request = objectstore.add(value); }); }
as said first case works second 1 inserts nothing , no error showed...
i think problem don't understand javascript types adequately i'm starting , i've spent whole evening it.
there's nothing wrong idb code. answer in code haven't presented, particularily ajax response (is json parsed way think is?)
be sure attach event listeners error
event. i'm positive if idb "inserts nothing" in fact it's not true "no error showed" , rather no error seen due callback mismanagement.
here's working implementation, modified previous answer i've given on tag. implementation doesn't have uniqueness constraints you've put on schema on purpose: shows looping fine. entries below good.
var db_name = 'so_22977915', store_name = 'people'; $.ajax({ url: '/echo/json/', type: 'post', datatype: 'json', data: { json: json.stringify({ case1: [{ "name": "nick", "age": 28 }, { "name": "katie", "age": 32 }], case2: [{ "husband": { "name": "jhon", "age": 23 }, "wife": { "name": "marie", "age": 24 } }] }) }, success: function (data) { var request, upgrade = false, dotx = function (db, entry) { adddata(db, entry, function () { getdata(db); }); }, getdata = function (db) { db.transaction([store_name], "readonly").objectstore(store_name).opencursor(idbkeyrange.lowerbound(0)).onsuccess = function (event) { var cursor = event.target.result; if (null !== cursor) { console.log("entry", cursor.value); cursor.continue(); } }; }, adddata = function (db, entry, finished) { console.log('adding', entry); var tx = db.transaction([store_name], "readwrite"), people = []; tx.addeventlistener('complete', function (e) { finished(); }); $.each(entry.case1, function (key, value) { tx.objectstore(store_name).add(value); }); $(entry.case2).each(function (key, value){ people.push(value.husband); people.push(value.wife); }); $.each(people, function (key, value) { tx.objectstore(store_name).add(value); }); }; request = window.indexeddb.open(db_name); request.oncomplete = function (event) { if (upgrade) { dotx(request.result, data); } }; request.onsuccess = function (event) { if (!upgrade) { dotx(request.result, data); } }; request.onupgradeneeded = function (event) { var db = event.target.result; db.createobjectstore(store_name, { keypath: null, autoincrement: true }); } } });
a cursor , console.log
shows entries being added:
Comments
Post a Comment