ionic cordovaSQLite plugin error Cannot read property 'openDatabase' of undefined

跟風遠走 提交于 2020-01-24 08:36:25

问题


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.



来源:https://stackoverflow.com/questions/27866396/ionic-cordovasqlite-plugin-error-cannot-read-property-opendatabase-of-undefine

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!