Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cannot call method 'transaction' of null #355

Closed
lucascardoso opened this issue Oct 2, 2015 · 20 comments
Closed

Cannot call method 'transaction' of null #355

lucascardoso opened this issue Oct 2, 2015 · 20 comments

Comments

@lucascardoso
Copy link

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!

@jonatas
Copy link

jonatas commented Oct 2, 2015

Is missing something like:

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

@lucascardoso
Copy link
Author

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;
       };

@anujgtm
Copy link

anujgtm commented Oct 4, 2015

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

@brodycj
Copy link
Contributor

brodycj commented Oct 8, 2015

Looks like an installation problem.

@lucascardoso
Copy link
Author

Hello @brodybits, and how could I solve this?

@brodycj
Copy link
Contributor

brodycj commented Oct 9, 2015

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.

@lucascardoso
Copy link
Author

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.

@brodycj
Copy link
Contributor

brodycj commented Oct 16, 2015

@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!

@lucascardoso
Copy link
Author

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.

@jnis77diver
Copy link

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)

@brodycj
Copy link
Contributor

brodycj commented Dec 24, 2015

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.

@brodycj brodycj closed this as completed Dec 24, 2015
@brodycj
Copy link
Contributor

brodycj commented Dec 24, 2015

I also published brodybits / Cordova-quick-start-checklist and brodybits / Cordova-troubleshooting-guide with this issue documented.

@borgbox
Copy link

borgbox commented Jan 3, 2016

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);

@fxbayuanggara
Copy link

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

@swastikpareek
Copy link

@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

@swastikpareek
Copy link

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.

@brodycj
Copy link
Contributor

brodycj commented Apr 28, 2016

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

@cisdev749
Copy link

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' });"

@egvrcn
Copy link

egvrcn commented Sep 11, 2016

Thanks @cisdev749 , this solved my problem

@kaszona
Copy link

kaszona commented Mar 20, 2017

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests