ionic cordovaSQLite plugin error Cannot read prope

2019-06-25 15:56发布

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?

3条回答
做个烂人
2楼-- · 2019-06-25 16:16

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.

查看更多
再贱就再见
3楼-- · 2019-06-25 16:20

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.

查看更多
女痞
4楼-- · 2019-06-25 16:30

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");
    }
查看更多
登录 后发表回答