gpt4 book ai didi

javascript - Uncaught ReferenceError : errorHandler is not defined at file

转载 作者:行者123 更新时间:2023-12-03 12:14:26 25 4
gpt4 key购买 nike

我收到错误:未捕获的 ReferenceError:errorHandler 未在文件中定义:

我做错了什么?源代码:http://pastebin.com/3203ynUB我用 onclick="startBackup()"启动第一部分

从数据库检索数据时似乎在某个地方出了问题,但我不知道如何以及在哪里。

数据库如下

数据库名称:SmartPassDB

表名称:SmartPass

行: id 、 name 、 nickName 、 passID 、 Website

// change to your database
var db = window.openDatabase("Database", "1.0", "SmartPassDB", 5*1024); // 5*1024 is size in bytes

// file fail function
function failFile(error) {
console.log("PhoneGap Plugin: FileSystem: Message: file does not exists, isn't writeable or isn't readable. Error code: " + error.code);
alert('No backup is found, or backup is corrupt.');
}

// start backup (trigger this function with a button or a page load or something)
function startBackup() {
navigator.notification.confirm('Do you want to start the backup? This will wipe your current backup. This action cannot be undone.', onConfirmBackup, 'Backup', 'Start,Cancel');
}

// backup confirmed
function onConfirmBackup(button) {
if(button==1) {
backupContent();
}
}

// backup content
function backupContent() {
db.transaction(
function(transaction) {
transaction.executeSql(
// change this according to your table name
'SELECT * FROM SmartPass;', null,
function (transaction, result) {
if (result.rows.length > 0) {
var tag = '{"items":[';
for (var i=0; i < result.rows.length; i++) {
var row = result.rows.item(i);
// expand and change this according to your table attributes
tag = tag + '{"id":"' + row.attribute1 + '","name":"' + row.attribute2 + '","nickName":"' + row.attribute3 + '","passId":"' + row.attribute4 + '","website":"' + row.attribute5 + '"}';
if (i+1 < result.rows.length) {
tag = tag + ',';

}
}
tag = tag + ']}';
window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, function(fileSystem) {
// Change the place where your backup will be written
fileSystem.root.getFile("backup.txt", {create: true, exclusive: false}, function(fileEntry) {
fileEntry.createWriter(function(writer) {
writer.write(tag);
}, failFile);
}, failFile);
}, failFile);
alert("Backup done.");
} else {
alert("No content to backup.");
}
},
errorHandler
);
}
);
}

// start restore (trigger this function with a button or a page load or something)
function startRestore() {
navigator.notification.confirm('Do you want to start the restore? This will wipe your current data. This action cannot be undone.', onConfirmRestore, 'Restore', 'Start,Cancel');
}

// restore confirmed
function onConfirmRestore(button) {
if(button==1) {
restoreContent();
}
}

// restore content
function restoreContent() {
db.transaction(
function(transaction) {
transaction.executeSql(
// change this according to your table name
'DELETE FROM SmartPass', startRestoreContent()
);
});
}

// actually start restore content
function startRestoreContent() {
window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, function(fileSystem) {
// Change the place where your backup is placed
fileSystem.root.getFile("backup.txt", null, function(fileEntry) {
fileEntry.file(function(file) {
var reader = new FileReader();
reader.onloadend = function(evt) {
var data = JSON.parse(evt.target.result);
var items = data.items;
count = items.length;
db.transaction(
function(transaction) {
$.each(items, function(index, item) {
transaction.executeSql(
// change and expand this according to your table name and attributes
'INSERT INTO SmartPass (id, name, nickName, passId, website) VALUES (?, ?, ?, ?, ?)',
[item.attribute1, item.attribute2, item.attribute3, item.attribute4, item.attribute5],
null
);
});
});
};
reader.readAsText(file);
alert("Restore done.");
}, failFile);
}, failFile);
}, failFile);
}

最佳答案

根据错误
错误:未捕获引用错误:errorHandler 未在文件中定义:
代码中定义函数errorHandler。在您的函数 backupContent() {..} 中,您已使用 errorHandler 作为 transaction.executeSql() 调用的回调引用。

transaction.executeSql(....,errorHandler)

您需要定义errorHandler函数。
此外,您还需要考虑如何处理初始数据库加载的场景。如果您第一次运行代码,则不会有任何表。下面的sql语句将会失败。

SELECT * FROM SmartPass;

SmartPass尚未创建。这是调用 errorHandler 的最可能原因。

关于javascript - Uncaught ReferenceError : errorHandler is not defined at file,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24792595/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com