0

I am doing a ajax call and store the response data into indexedDB. The response is an array of strings. After checking many questions and suggestions, i have followed the answer of @buley and did like this below :

 $(document).ready(function () {
var db_name = 'mobileClaimsDb',
    store_name = 'claims';
$.ajax({
    url: 'index.cfm?action=main.appcache',
    type: 'post',
    cache:true,
    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"),
                    claims = [];
                tx.addEventListener('complete', function (e) {
                    finished();
                });
                $.each(claims, 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('claims', {
                keyPath: null,
                autoIncrement: true
            });
        }
    }
}); 

});

It is creating the db and store too but data is not showing while opening the store.But, it is showing the data in console with key/value pair. I am not understand why it is not showing.enter image description here

It is showing like this.

In console, it is populating properly. enter image description here

What changes i need to do to show the key and value pair in store.

learningMonk
  • 1,101
  • 13
  • 34
  • 1
    `claims = []` and then `$.each(claims...`. – Gabriel Apr 10 '20 at 14:25
  • Thanks gabriel . I missed that one. Yes it is working – learningMonk Apr 10 '20 at 14:55
  • @gabriel can you please give some suggestion how to retrieve those data in serviceworker? – learningMonk Apr 10 '20 at 15:04
  • It's the same as doing it from a regular web page. See: https://stackoverflow.com/questions/29206836/accessing-indexeddb-in-serviceworker-race-condition https://github.com/GoogleChrome/samples/blob/gh-pages/service-worker/offline-analytics/service-worker.js – Gabriel Apr 11 '20 at 03:08
  • Thank you so much gabriel. I have one query so i opened a question for this. https://stackoverflow.com/questions/61193797/how-to-cache-ajax-response-by-using-serviceworker Can you please check this and suggest me what are the changes needed or any helpful info for this? – learningMonk Apr 14 '20 at 10:01

0 Answers0