未捕获的ReferenceError:未在文件中定义errorHandler

时间:2014-07-16 23:36:24

标签: javascript jquery cordova

我收到错误:Uncaught ReferenceError:errorHandler未在文件中定义:

我在做错了什么?源代码:http://pastebin.com/3203ynUB 我用onclick =“startBackup()”

来修改第一部分

从数据库中检索数据似乎出错了,但我无法弄清楚如何以及在何处。

数据库如下

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

1 个答案:

答案 0 :(得分:1)

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

transaction.executeSql(....,errorHandler)

您需要定义 errorHandler 功能 此外,您还需要考虑如何处理初始数据库负载的方案。如果您运行第一次时间的代码,则不会有任何表格。以下sql语句将失败。

SELECT * FROM SmartPass;

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