Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ionic Cordova SQLite plugin error Cannot read property 'openDatabase' of undefined

I have 1 issue in ngCordova plugin cordovaSQLite. Below code:

var db = $cordovaSQLite.openDB({ name: "myDB.db" });

I'm using ionic serve in browser.

Error:

Uncaught TypeError: Cannot read property 'openDatabase' of undefinedng-cordova.js:5058 
openDBapp.js:27 
(anonymous function)ionic.bundle.js:37388 
(anonymous function)ionic.bundle.js:2241 
onPlatformReadyionic.bundle.js:2220 
onWindowLoad

enter image description here

Could you please help me?

like image 493
Dhaval Kumar Avatar asked Dec 19 '22 07:12

Dhaval Kumar


2 Answers

As stated by user jonnie in his answer

Cordova is platform specific and doesn't work when you run ionic serve

and

you can replace the cordova plugin with window to use the websql databases so instead of sqlitePlugin.openDatabase() you can use window.openDatabase()

It should work on actual devices just fine.

like image 52
Roope Hakulinen Avatar answered Dec 28 '22 06:12

Roope Hakulinen


You are getting this error because you are trying to get database connection from android but using wrong function openDatabase(). This function will use in case of client (browser). Use openDb() function to get database connection from android.

 if (window.cordova) {
       db = $cordovaSQLite.openDB({ name: "my.db" }); //device
            console.log("Android");
     }
     else{
       db = window.openDatabase("my.db", '1', 'my', 1024 * 1024 * 100); // browser
       console.log("browser");
    }
like image 30
Kooldandy Avatar answered Dec 28 '22 05:12

Kooldandy