angularjs - Inserting duplicate value in indexeddb -
when insert on database , inserting same data twice.
table create
var ooptions = { keypath: account.primarykey, autoincrement: true }; var ostore = dbhandle.createobjectstore(account.tablename, ooptions); var oixoptions = { unique: false }; account.fields.foreach(function(item) { ostore.createindex(item + "index", item, oixoptions); });
insert
var defered = $q.defer(); try { var objectstore = config.database.transaction(tablename, "readwrite").objectstore(tablename); var result = objectstore.add(entity); result.onerror = function(e) { defered.reject("can't insert account"); throw e; } result.onsuccess = function(e) { defered.resolve(); } } catch (e) { defered.reject("can't insert account"); throw e; } return defered.promise;
retrive
var defered = $q.defer(); try { var req = $window.indexeddb.open(config.databasename, 1.0); req.onsuccess = function(evt) { config.database = evt.target.result; var transaction = config.database.transaction(account.tablename, idbtransaction.read_only); var objectstore = transaction.objectstore(account.tablename); var tmpdata = []; objectstore.opencursor().onsuccess = function(event) { var cursor = event.target.result; if (!cursor) { defered.resolve(tmpdata); return; } tmpdata.push(cursor.value); cursor.continue(); }; } } catch (e) { defered.reject("can't pull account"); throw e; } return defered.promise;
any suggestion ??
this might less of problem indexeddb , more problem use of try/catch , promises. have tested without try/catch , without promises? rationale using promises here? consider don't need try/catch , don't need promises perform these simple tasks.
Comments
Post a Comment