ionic cordovaSQLite plugin error Cannot read prope

2019-06-25 16:24发布

问题:

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

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

i'm using "ionic serve" for run project 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

[Main error] http://i.stack.imgur.com/Yctpl.png

Could you please help me?

回答1:

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.



回答2:

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


回答3:

You will not get the desired result if you test in a web browser. You need an actual device, be it an Android or iOS. You can you use MEMU or Bluestack emulator to install and check if you don't want an actual device. Please don't consider other steps, that's a waste of time.