javascript - How to properly retrieve all data from an IndexedDb in a WinJS Windows 8 app? -



javascript - How to properly retrieve all data from an IndexedDb in a WinJS Windows 8 app? -

i've been googling while , have found nil can help me. i'm trying retrieve info indexeddb.

here's code looks far:

var request = window.indexeddb.open("list", 1); request.onsuccess = function (event) { db = request.result; console.log("success: " + db); }; request.onupgradeneeded = function (event) { db = event.target.result; var objectstore = db.createobjectstore("list", { keypath: "id" }); (var in parseddata) { objectstore.add(parseddata[i]); } console.log("retrieving data"); var transaction = request.transaction("list"); var store = transaction.objectstore("list"); store.opencursor().onsuccess = function (e) { var cursor = e.target.request; if (cursor) { console.log("list names == " + cursor.value.name); } } }

now, there's couple things need said code have written. first of all, i'm aware retrieving info should in function of own (i wrote way printing/logging purposes only). second, reason, "retrieving data" should @ to the lowest degree print log -- doesn't , don't know why. 3rd - , importantly - maintain getting error on line:

var transaction = request.transaction("list");

the error is: javascript runtime error: function expected

not sure causing these 2 problems i'm having.

the onupgradeneeded callback called when increment version parameter indexeddb.open. need increment, save, , reload each time want illustration code run. should cause retrieving info command printed. far easier test if did not seek , insert/read info within onupgradeneeded callback function. way not need increment database version every time wanted test/run code. you need open transaction using database object. right state var transaction = request.transaction('list');. request idbrequest. don't think there idbrequest.prototype.transaction method. want utilize idbdatabase.prototype.transaction. example, var transaction = db.transaction('list');. alternatively #2, more complicated, can utilize implicit transaction used part of onupgradeneeded. special version_change type transaction created automatically indexeddb valid within body of onupgradeneeded callback. version_change txs back upwards read/write. if want this, utilize var transaction = event.target.transaction;. not recommend possible. in cursor onsuccess callback, looks not advancing cursor past first record. need add together cursor.continue(); within if block go on iteration.

a simple illustration of how avoid having upgrade database each time:

function upgradedatabase(event) { var db = event.target.result; db.createobjectstore('list', { keypath: "id" }); } function addparseddata(parseddata, callback) { var request = indexeddb.open('list', 1); request.onupgradeneeded = upgradedatabase; request.onsuccess = function(event) { var db = event.target.result; var tx = db.transaction('list','readwrite'); tx.oncomplete = callback; var list = tx.objectstore('list'); for(var in parseddata) { list.add(parseddata[i]); } }; } function retrieve() { var request = indexeddb.open('list', 1); request.onupgradeneeded = upgradedatabase; request.onsuccess = function(event) { var db = event.target.result; var tx = db.transaction('list'); var list = tx.objectstore('list'); var getall = list.opencursor(); var items = []; tx.oncomplete = function() { console.log('got items %o', items); }; getall.onsuccess = function(event) { var cursor = event.target.result; if(!cursor) return; items.push(cursor.value); cursor.continue(); }; }; } // connect, create, insert, , select addparseddata(parseddata, retrieve);

javascript windows windows-8 winjs indexeddb

Comments

Popular posts from this blog

xslt - DocBook 5 to PDF transform failing with error: "fo:flow" is missing child elements. Required content model: marker* -

mediawiki - How do I insert tables inside infoboxes on Wikia pages? -

Local Service User Logged into Windows -