Angularjs 在indexeddb中插入重复值

Angularjs 在indexeddb中插入重复值,angularjs,ionic,indexeddb,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(it

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

表格创建

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 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;
插入

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;
检索

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

有什么建议吗

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

你的插入方法被调用了两次,还是插入两次?是什么触发了对insert方法的调用?这与双重插入无关,但是。。。您是否在使用承诺中包装的try/catch块?@tpie是的,我不应该在承诺期间使用try-ctach吗?承诺允许代码块异步运行,但try/catch显然是同步和阻塞的。如果一个请求挂起在你的try块中,你的应用程序将挂起。我正在IE上测试它。。在Ionic IE中,为ng点击触发事件两次。在过滤了触发器的数量后,我解决了这个问题。