【发布时间】:2017-05-17 11:38:01
【问题描述】:
我正在努力解决 Phonegap/Cordova 的错误。我正在连接的设备中构建和测试我的应用程序,这是我得到的错误:
INVALID_STATE_ERR:DOM 异常 11
我找到的解决方案都不起作用。他们中的大多数都是针对使用ajax的,这里不是这样。我发现了另一个关于 phonegap 的问题,他们说这可能是因为设备还没有准备好,但我已经检查过设备在我的情况下是否准备好了。
这是引发错误的代码(顺便说一句,我正在使用 Promise polyfill,因为目前 Cordova 不支持它):
/**
* Clients insertion
* @param tx
* @param clientes
* @return {Promise}
*/
clientes = function(clientes) {
return new Promise(function(resolve, reject) {
var clientesCount = clientes.length;
if (clientesCont === 0){
resolve();
}
else {
APP.db.transaction(function(tx) {
$.each(clientes, function(i, cliente) {
var idclienteLocal;
// Current client
tx.executeSql("INSERT OR REPLACE INTO clientes (id, name, ...) " +
"VALUES " +
"(?,?,...)",
[cliente.id, cliente.name],
function(tx, res) {
clientesCount--;
idclienteLocal = res.insertId;
// Clien phones
telefonosCliente(tx, cliente, idclienteLocal).then(function() {
// Client credits
creditosCliente(tx, cliente, idclienteLocal).then(function() {
if (clientesCount === 0) {
resolve();
}
}).catch(function(error) {
reject('Error créditos cliente ' + cliente.empresa + ': ' + error);
});
}).catch(function(error) {
reject('Error teléfonos cliente ' + cliente.empresa + ': ' + error);
});
}, function(tx, error) {
reject(error.message);
});
});
});
}
});
}
我怀疑它可能与循环有关(我为此使用了 jQuery),所以我尝试使用递归函数在前一个迭代完全解决时对每个迭代进行排队,但它没有甚至在浏览器中工作(第一个 procesarCliente() 调用中的 Promise,在 clientes() 中不会被解析)。这是修改后的代码:
/**
* Recursive function to process each client one after the other
* @param tx
* @param clientes
* @param cliente
* @return {Promise}
*/
procesarCliente = function(tx, clientes, cliente) {
return new Promise(function(resolve, reject) {
// Current client
tx.executeSql("INSERT OR REPLACE INTO clientes (id, name, ...) " +
"VALUES " +
"(?,?,...)",
[cliente.id, cliente.name,...],
function(tx, res) {
var idclienteLocal = res.insertId;
// Client phones
telefonosCliente(tx, cliente, idclienteLocal).then(function() {
// Client credits
creditosCliente(tx, cliente, idclienteLocal).then(function() {
if (clientes.length === 0) {
resolve();
}
else {
procesarCliente(tx, clientes, clientes.shift());
}
}).catch(function(error) {
reject('Error créditos cliente ' + cliente.empresa + ': ' + error);
});
}).catch(function(error) {
reject('Error teléfonos cliente ' + cliente.empresa + ': ' + error);
});
}, function(tx, error) {
reject(error.message);
});
});
},
/**
* Clients insertion
* @param tx
* @param clientes
* @return {Promise}
*/
clientes = function(clientes) {
return new Promise(function(resolve, reject) {
var cliente,
clientesCont = clientes.length;
if (clientesCont === 0){
resolve();
}
else {
APP.db.transaction(function(tx) {
cliente = clientes.shift();
procesarCliente(tx, clientes, cliente).then(function() {
resolve();
}).catch(function(error) {
reject(error);
});
});
}
});
}
对修复第二种方法有什么帮助,最重要的是,如何让它在 phonegap 中工作?
编辑
我添加了一个验证码来检查本地数据库中的每个表,令人惊讶的是,它显然只创建了两个名为“未定义”和“项目”的表(我什至没有使用这样的表)。
这里是验证码:
if (APP.DEBUG) { // this is true
// Verify inserted data when triggered event 'app.load.all' just after the very last item in the DB has been inserted
$wrapper.on('app.load.all', function() {
APP.db.transaction(function(tx) {
console.log('Verifying DB');
tx.executeSql("SELECT name FROM sqlite_master WHERE type='table'", [],
function(tx, res) {
if (res.rows.length) {
$.each(res.rows, function(i, tabla) {
if (tabla.name !== '__WebKitDatabaseInfoTable__') {
console.log('Verifying table ' + tabla.name);
tx.executeSql("SELECT * FROM " + tabla.name, [],
function(tx, res) {
console.log("Table " + tabla.name + " has " + res.rows.length + " records");
},
function(tx, error) {
console.log('Error verifying table ' + tabla.name + ':' + error.message);
});
}
});
}
}, function(tx, error) {
console.log('Error verifying DB tables: ' + error.message);
});
});
});
}
这会导致这些行被“Web 控制台”过滤:
I/Web Console(14391): Verifying DB:1085
I/Web Console(14391): Verifying table undefined:1091
I/Web Console(14391): Verifying table item:1091
I/Web Console(14391): Error Verifying table undefined:no such table: undefined:1100
I/Web Console(14391): Error Verifying table item:no such table: item:1100
这对我来说完全陌生
【问题讨论】:
标签: javascript sqlite cordova promise web-sql