在indexeddb

时间:2015-04-22 16:52:06

标签: angularjs ionic indexeddb

当我在我的数据库中插入时,它会插入两次相同的数据。

表创建

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);
});

插入

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 into account");
        throw e;
    }
    result.onsuccess = function(e) {
        defered.resolve();
    }
} catch (e) {
    defered.reject("Can't insert into 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 from account");
    throw e;
}
return defered.promise;

有什么建议吗?

1 个答案:

答案 0 :(得分:1)

这可能不是indexedDB的问题,而是使用try / catch和promises的问题。你有没有尝试/捕获和没有承诺测试?你在这里使用承诺的理由是什么?考虑到你不需要try / catch,你不需要承诺来执行这些简单的任务。