我如何测试连接。我正在使用javascript与数据库集成 [英] How do I a test conection. I´m using javascript with database integration

查看:67
本文介绍了我如何测试连接。我正在使用javascript与数据库集成的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

错误 - >第43行index.js - >未捕获的TypeError:无法读取属性'querySelector'的null



所以,我尝试了测试连接。看起来这个页面没有数据库集成。



Error --> Line 43 index.js -->Uncaught TypeError: Cannot read property 'querySelector' of null

So, I have try a test connection. It´s look that don´t have a database integration in this page.

var db = null;
var db2 = null;
var db3 = null;
var dbUser = null;
var dbName = "estudos.db";

var app = {
    // Application Constructor
    initialize: function() {
        document.addEventListener('deviceready', this.onDeviceReady.bind(this), false);
    },

    // deviceready Event Handler
    //
    // Bind any cordova events here. Common events are:
    // 'pause', 'resume', etc.
    onDeviceReady: function() {
        this.receivedEvent('deviceready');
    },

    // Update DOM on a Received Event
    receivedEvent: function(id) {
        var parentElement = document.getElementById(id);
      43-->>  var listeningElement = parentElement.querySelector('.listening');
        var receivedElement = parentElement.querySelector('.received');

        listeningElement.setAttribute('style', 'display:none;');
        receivedElement.setAttribute('style', 'display:block;');


        // OPERACOES BD - inicio

        //banco de dados local - aceite de termos e outras coisas
        dbUser = window.sqlitePlugin.openDatabase({name: 'user.db', location: 'default'});
        dbUser.transaction(function(tx) {
            tx.executeSql('CREATE TABLE IF NOT EXISTS Users (flg_aceite, flg_valid_bd)');
        }, function(error) {
            alert('Transaction ERROR: ' + error.message);
        }, function() {
            console.log('Database OK');
        });

        //copia do banco de dados de estudos
        window.plugins.sqlDB.copy(dbName, 0, copysuccess, copyerror);
        // OPERACOES BD - fim
    }
};

app.initialize();

//---------------------------------------------------------------

function copysuccess()
{
    //primeira versão deste banco de dados. o comando anterior.
    //provavelmente realizou a cópia, abro o BD.
    db = window.sqlitePlugin.openDatabase({name: dbName});
    //preciso verificar se existem versões anteriores deste BD. Deleto por precaucao
    dropTable();
    fts_table();
}

function copyerror(e)
{
    //esta versao do banco de dados ja existe.
    //abro o BD
    db = window.sqlitePlugin.openDatabase({name: dbName});
    //db3 = window.sqlitePlugin.openDatabase({name: "vtestudos"});
    //alert("copyerror" + JSON.stringify(e));
}


//---------------------------------------------------------------

function fts_table(){
    db.transaction(function(tx) {
    tx.executeSql('CREATE VIRTUAL TABLE vtestudos USING FTS3(titulo, texto, id_titulo)', [], function(tx,res){
          //alert("nao deu erro");
          //db = window.sqlitePlugin.openDatabase({name: "vtestudos"});
          //alert("uai. deu pra abrir");

          db.transaction(function(tx) {
          tx.executeSql('INSERT INTO vtestudos(titulo, texto, id_titulo) SELECT titulo, texto, id_titulo FROM estudos', [], function(tx,res){
              //db3 = window.sqlitePlugin.openDatabase({name: "vtestudos"});
               console.log('insert ok');
          });
          }, function(err){
              alert(err.message);
          });

    });
    }, function(err){
        alert(err.message);
    });
}

//---------------------------------------------------------------

function dropTable()
{
    window.plugins.sqlDB.remove("estudosprev1", 0, rmsuccess,rmerror); 
    window.plugins.sqlDB.remove("estudosprev2", 0, rmsuccess,rmerror);  
}

function rmsuccess()
{
    //existe versão anterior
    //alert("removesuccess");
    console.log('existe versão anterior');
}

function rmerror(e)
{
    //não existe versão anterior. ignoro.
    //alert("removeerror" + JSON.stringify(e));
    console.log('n existe versão anterior. ignoro.');
}

//---------------------------------------------------------------

/*
function displayNote(name)
{
db.transaction(function(tx) {
    tx.executeSql('SELECT * FROM estudos', [], function(tx,res){
          alert(res.rows.item(0).titulo);
          //alert(res.rows.item(0).texto);

    });
}, function(err){
    alert(err.message);
    alert("An error occured while displaying the note");
});
}
*/





我尝试了什么:



这只是一个测试连接。我试过这个:





What I have tried:

It´s only a test connection.I have tried this:

document.addEventListener('deviceready', function() {
  window.sqlitePlugin.echoTest(function() {
    alert('ECHO test OK');
  });
});

document.addEventListener('deviceready', function() {
  window.sqlitePlugin.selfTest(function() {
    alert('SELF test OK');
  });
});

推荐答案

引用:

未捕获TypeError:无法读取属性'querySelector'为null

Uncaught TypeError: Cannot read property 'querySelector' of null



如果再次阅读该消息,可能看到读取属性'querySelector'失败,因为 parentElement null

And parentElement null 因为 document.getElementById(id)失败,它可能会失败,因为 id 值不是你的元素的 id 页面。



如果你不明白你的代码在做什么或为什么它做了什么,答案是调试器

使用调试器查看代码正在执行的操作。只需设置断点并查看代码执行情况,调试器允许您逐行执行第1行并在执行时检查变量,这是一个令人难以置信的学习工具。



调试器 - 维基百科,免费的百科全书 [ ^ ]

掌握Visual Studio 2010中的调试 - 初学者指南 [ ^ ]

使用Visual Studio 2010进行基本调试 - YouTube [ ^ ]



调试器在这里向您展示您的代码在做什么你的任务是与它应该做的事情进行比较。

调试器中没有魔法,它没有发现错误,它只是帮助你。当代码没有达到预期的效果时,你就会接近一个错误。


If you read the message again, may see that "read property 'querySelector'" fails because parentElementis null.
And parentElement is null because document.getElementById(id) fails, and it may fail because id value is not the id of an element of your page.

When you don't understand what your code is doing or why it does what it does, the answer is debugger.
Use the debugger to see what your code is doing. Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute, it is an incredible learning tool.

Debugger - Wikipedia, the free encyclopedia[^]
Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
Basic Debugging with Visual Studio 2010 - YouTube[^]

The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't find bugs, it just help you to. When the code don't do what is expected, you are close to a bug.


这篇关于我如何测试连接。我正在使用javascript与数据库集成的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆