storesafe / cordova-sqlite-storage

A Cordova/PhoneGap plugin to open and use sqlite databases on Android, iOS and Windows with HTML5/Web SQL API

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Cannot call method 'transaction' of null

lucascardoso opened this issue · comments

Hi guys, I'm having a problem when it comes to running a query in the database.

this is the query that is executed:

var query = "SELECT email FROM usuarios WHERE email = ?";
var existe = false;
$cordovaSQLite.execute(db, query, [usuario]).then(function (res) {
if (res.rows.length > 0) {
existe = true;
} else {
console.log("Nenhum resultado encontrado.");
}
}, function (err) {
console.error(err);
});

this is the error I get:
D/SystemWebChromeClient(23723): file:///android_asset/www/lib/ionic/js/ionic.bundle.js: Line 21157 : TypeError: Cannot call method 'transaction' of null
D/SystemWebChromeClient(23723): at Object.execute (file:///android_asset/www/lib/ngCordova/dist/ng-cordova.js:5960:12)
D/SystemWebChromeClient(23723): at findUsuario (file:///android_asset/www/js/servico.js:16:22)
D/SystemWebChromeClient(23723): at efetuarLogin (file:///android_asset/www/js/servico.js:37:13)
D/SystemWebChromeClient(23723): at Scope.$scope.login (file:///android_asset/www/js/controllers/controllers.js:55:47)
D/SystemWebChromeClient(23723): at fn (eval at (file:///android_asset/www/lib/ionic/js/ionic.bundle.js:1:0), :4:206)
D/SystemWebChromeClient(23723): at file:///android_asset/www/lib/ionic/js/ionic.bundle.js:57514:9
D/SystemWebChromeClient(23723): at Scope.$eval (file:///android_asset/www/lib/ionic/js/ionic.bundle.js:24673:28)
D/SystemWebChromeClient(23723): at Scope.$apply (file:///android_asset/www/lib/ionic/js/ionic.bundle.js:24772:23)
D/SystemWebChromeClient(23723): at HTMLButtonElement. (file:///android_asset/www/lib/ionic/js/ionic.bundle.js:57513:13)
D/SystemWebChromeClient(23723): at HTMLButtonElement.eventHandler (file:///android_asset/www/lib/ionic/js/ionic.bundle.js:12098:21)
I/chromium(23723): [INFO:CONSOLE(21157)] "TypeError: Cannot call method 'transaction' of null
I/chromium(23723): at Object.execute (file:///android_asset/www/lib/ngCordova/dist/ng-cordova.js:5960:12)
I/chromium(23723): at findUsuario (file:///android_asset/www/js/servico.js:16:22)
I/chromium(23723): at efetuarLogin (file:///android_asset/www/js/servico.js:37:13)
I/chromium(23723): at Scope.$scope.login (file:///android_asset/www/js/controllers/controllers.js:55:47)
I/chromium(23723): at fn (eval at (file:///android_asset/www/lib/ionic/js/ionic.bundle.js:1:0), :4:206)
I/chromium(23723): at file:///android_asset/www/lib/ionic/js/ionic.bundle.js:57514:9
I/chromium(23723): at Scope.$eval (file:///android_asset/www/lib/ionic/js/ionic.bundle.js:24673:28)
I/chromium(23723): at Scope.$apply (file:///android_asset/www/lib/ionic/js/ionic.bundle.js:24772:23)
I/chromium(23723): at HTMLButtonElement. (file:///android_asset/www/lib/ionic/js/ionic.bundle.js:57513:13)
I/chromium(23723): at HTMLButtonElement.eventHandler (file:///android_asset/www/lib/ionic/js/ionic.bundle.js:12098:21)", source: file:///android_asset/www/lib/ionic/js/ionic.bundle.js (21157)

if anyone can help me right now thanks!

Is missing something like:

 db = $cordovaSQLite.openDB("my.db");
$cordovaSQLite.execute(db,...

I'm opening the db:

         if(window.cordova) {
           db = $cordovaSQLite.openDB("appViajantes.db"); //app syntax
         } else {
           db = window.openDatabase("appViajantes.db", "1.0", "App Viajantes", 2 * 1024 * 1024); // ionic     serve syntax
         }

       db.transaction(function (tx) {
         tx.executeSql(sqlEstado, [], success, error);
         tx.executeSql(sqlCidade, [], success, error);
         tx.executeSql(sqlBairro, [], success, error);
         tx.executeSql(sqlLocal, [], success, error);
         tx.executeSql(sqlUsuarios, [], success, error);
       });

       function success (tx, result) {
         console.log("Criou com sucesso");
       };

       function error (tx, error) {
         console.log("ERROR na criação do banco: " + error.message);
         return true;
       };

I am also getting the same error as soon as application loads.

Looks like an installation problem.

Hello @brodybits, and how could I solve this?

Please start with a fresh, clean project and run a very simple test program. You may have to try one thing at a time to find out what is causing the problem.

Hello @brodybits, can solve this error. What was happening to me was a keyboard error before initializing the database so db was nil. I installed the plugin ionic-plugin-keyboard keyboard.

Thank you for your help.

@lucascardoso happy to hear! Can you do us a favor and describe how you the keyboard error happened?

I would like to get this documented somewhere since it may help others. Thanks!

The error appeared that every time the application initializing. This was the error:
ionic Uncaught TypeError: Can not read property 'Keyboard' of undefined
I did not think it podesse solve my problem transaction, but after installing the keyboard plugin worked perfectly.

Thank you @brodybits.

I had the same exact problem as @lucascardoso where I was getting the error below (output from Genymotion with the 'adb logcat' command. Tried everything. Most of a day wasted on this. The fix was adding the ionic-plugin-keyboard

I/chromium( 1681): [INFO:CONSOLE(21162)] "TypeError: Cannot call method 'transaction' of null I/chromium( 1681): at Object.execute (file:///android_asset/www/lib/ngCordova/dist/ng-cordova.js:6378:12) I/chromium( 1681): at file:///android_asset/www/app/yards/yardsCtrl.js:30:32 I/chromium( 1681): at file:///android_asset/www/lib/ionic/js/ionic.bundle.js:48817:19 I/chromium( 1681): at Object.ionic.Platform.ready (file:///android_asset/www/lib/ionic/js/ionic.bundle.js:2122:9) I/chromium( 1681): at Object.self.ready (file:///android_asset/www/lib/ionic/js/ionic.bundle.js:48815:26) I/chromium( 1681): at Scope.$scope.insert (file:///android_asset/www/app/yards/yardsCtrl.js:29:28) I/chromium( 1681): at fn (eval at <anonymous> (file:///android_asset/www/lib/ionic/js/ionic.bundle.js:21977:15), <anonymous>:4:209) I/chromium( 1681): at file:///android_asset/www/lib/ionic/js/ionic.bundle.js:57606:9 I/chromium( 1681): at Scope.$eval (file:///android_asset/www/lib/ionic/js/ionic.bundle.js:24678:28) I/chromium( 1681): at Scope.$apply (file:///android_asset/www/lib/ionic/js/ionic.bundle.js:24777:23)", source: file:///android_asset/www/lib/ionic/js/ionic.bundle.js (21162)

I just added a note to README.md and raised a question on the Ionic forum at: https://forum.ionicframework.com/t/keyboard-error-and-cordova-plugins/39851 (edited to add a question if I should raise this on the Ionic and/or Ionic2 project on Github)

I am waiting for an answer from Ionic. Guys it would help if you can add comment(s) of support to my question and/or raise an issue on Github if you think it is appropriate.

Closing since this does not seem to be fault of this plugin.

I had the same issue, after a long search i found out that the root cause is in the "$ionicPlatform.ready", reach this method doesn't mean that the device is actually ready, this is an issue especially when dealing with cordova plugins. check the reference in the link below:

http://gyf1.com/blog/2015/06/22/initialize-angularjs-app-after-deviceready/

I solved my issue by using "document.addEventListener" in the beginning of my controller to start my database:

document.addEventListener("deviceready", function () {
if (window.cordova) {
db = $cordovaSQLite.openDB({ name: "my.db" }); //device
}else{
db = window.openDatabase("my.db", '1', 'my', 1024 * 1024 * 100); // browser
}
//Cria a tabela e BD se não existirem. Obs.: a inicialização tem de estar dentro do método $ionicPlatform.ready
// db = $cordovaSQLite.openDB({name: "my.db"});
$cordovaSQLite.execute(db, "CREATE TABLE IF NOT EXISTS notas (id INTEGER PRIMARY KEY, title text, description text)");

$scope.notes = [];
NoteStore.list($scope.notes).then(function(notes){
$scope.notes = notes;
});

}, false);

sometimes do reinstall platform is working... ionic platform rm android, ionic platform add android

@brodybits : I am having the same issue.
TypeError: Cannot call method 'transaction' of null
at Object.execute (file:///android_asset/www/js/ng-cordova.min.js:9:19175)
at file:///android_asset/www/js/services.js:10:32
at file:///android_asset/www/lib/ionic/js/ionic.bundle.js:48817:19
at Object.ionic.Platform.ready (file:///android_asset/www/lib/ionic/js/ionic.bundle.js:2122:9)
at Object.self.ready (file:///android_asset/www/lib/ionic/js/ionic.bundle.js:48815:26)
at Object.self.query (file:///android_asset/www/js/services.js:9:28)
at Object.self.get (file:///android_asset/www/js/services.js:49:24)
at file:///android_asset/www/js/controllers.js:54:22
at processQueue (file:///android_asset/www/lib/ionic/js/ionic.bundle.js:23399:28)
at file:///android_asset/www/lib/ionic/js/ionic.bundle.js:23415:27

I have installed cordova 6.1.0 and ionic 1.7.14 . I have ageGate Validation function that just checks age and then runs the code -

    $scope.above18 = function() {
        var user = CreateUser.create($rootScope.uderData).then(function(result) {
            Data.get("id").then(function(response) {
                $cordovaSQLite.execute(db, "DROP TABLE IF EXISTS drinkasaur");
                $cordovaSQLite.execute(db, "CREATE TABLE IF NOT EXISTS drinkasaur (key text primary key, value text)");
                var newObj = {};
                newObj.key = "id";
                newObj.name = result.data.user._id;
                var newObj1 = {};
                newObj1.key = "email";
                newObj1.name = $rootScope.uderData.email;
                $scope.newDbEntery($rootScope.objName);
                $scope.newDbEntery($rootScope.objPic);
                $scope.newDbEntery(newObj);
                $scope.newDbEntery(newObj1);
                $rootScope.showHeader = true;
                $location.path("/landingPage");

            }, function(error) {
                var newObj = {};
                newObj.key = "id";
                newObj.name = result.data.user._id;
                var newObj1 = {};
                newObj1.key = "email";
                newObj1.name = $scope.userObj.email;
                $scope.newDbEntery($rootScope.objName);
                $scope.newDbEntery($rootScope.objPic);
                $scope.newDbEntery(newObj);
                $scope.newDbEntery(newObj1);
                $rootScope.showHeader = true;
                $location.path("/landingPage");
            });
        }, function(error) {});
    }

I when I call this function this js error comes.

Please help me out

Hey . I was able to fix the bug . It was not related to any specific cordova plugin but the cordova version.

I have recently upgraded my cordova version from 5.x.x to 6.x.x which was a break release (unfortunately) and openDB function which was initially taking database name as a whole and sole required parameter needed to have one more argument 'location' clubbed with database name as json ..

Due to this upgradation to break release my db var was getting a null value; and hence this problem.

So $cordovaSQLite.openDB("myapp9.db"); needed to be changed with $cordovaSQLite.openDB({name:"myapp9.db", location:'default'});

Changing this solved my problem.

Thanks @swastikpareek, this definitely needs to be documented. I raised #471 to track this.

Hi there,
if you use $cordovaSQLite.openDB to create db then you need to have two parameters first name and second is location pls do this to solve the problem "db = $cordovaSQLite.openDB({ name: 'app.db',location: 'default' });"

Thanks @cisdev749 , this solved my problem

This is due to you are trying to execute another excecutesql or transaction before finishing of current transaction.