I've read these questions:
- JavaScript closure inside loops – simple practical example
- How do JavaScript closures work?
- How do I pass the value (not the reference) of a JS variable to a function?
and tried to apply their solutions (as well as at least 1/2 a dozen other implementations) and none of them are working.
Here's the function that has the loop:
ExecuteQueryWhereQueryAndParamsBothArrays: function (queryArray, paramsArray, idsArray, success, fail, errorLogging) {
var hasError = false;
$rootScope.syncDownloadCount = 0;
$rootScope.duplicateRecordCount = 0;
$rootScope.db.transaction(function (tx) {
for (var i = 0; i < paramsArray.length; i++) {
window.logger.logIt("id: " + idsArray[i]);
var query = queryArray[i];
var params = paramsArray[i];
var id = idsArray[i];
tx.executeSql(query, params, function (tx, results) {
incrementSyncDownloadCount(results.rowsAffected);
}, function(tx, error) {
if (error.message.indexOf("are not unique") > 0 || error.message.indexOf("is not unique") > 0) {
incrementDuplicateRecordCount(1);
return false;
}
// this didn't work: errorLogging(tx, error, id);
// so I wrapped in in an IIFE as suggested:
(function(a, b, c) {
errorLogging(a, b, idsArray[c]);
})(tx, error, i);
return true;
});
}
}, function () {
fail();
}, function () {
success();
});
And here's the errorLogging function that is writing my message (Note, I'm not able to "write" the message in the same javascript file because I'd need to [angular] inject another reference into this file and it would cause a circular reference and the code won't run)
var onError = function (tx, e, syncQueueId) {
mlog.LogSync("DBService/SQLite Error: " + e.message, "ERROR", syncQueueId);
};
What other method can I implement to stop it from returning the very last "id" of my sync records (when it's only the first record that has the error)?
Question&Answers:os