an-rahulpandey / cordova-plugin-dbcopy

Copy SQLite Database from www folder to default app database location
Apache License 2.0
89 stars 48 forks source link

window.plugins.sqlDB not defined in Ionic View on iOS #29

Closed michal-th closed 7 years ago

michal-th commented 8 years ago

I've been trying to identify the issue for past 8 hours - my code works correctly on Android ionic view, however iOS is throwing

"undefined is not an object (evaluating 'window.plugins.sqlDB.copy') "

I tried wrapping it in a timeout (as there is an issue of delayed loading of plugins in ionic view) with as much as 5 seconds delay, to no avail.

I've installed the plugin via : cordova plugin add https://github.com/an-rahulpandey/cordova-plugin-dbcopy.git

My code in app.js

.run(function($ionicPlatform, $rootScope, $ionicHistory, $state, $mdDialog, $mdBottomSheet, $cordovaPush, $cordovaSQLite, DB, $timeout,$ionicLoading, $window) {
  $ionicPlatform.ready(function(){
    $timeout(function(){
           try{
               window.plugins.sqlDB.copy("mydb.db",0, function () {
                alert('ok')
                console.log("copy ok")
            },function(e){
                alert(e)
                console.log("copy fail")
                console.log(e)
            });
          }catch(e){
                alert(e);
                console.log(e);
                console.log(e.stack);
                console.log(e.line);
            }

     }, 5000);
  })
})
an-rahulpandey commented 8 years ago

Does the cordova_plugins.js file contains the entry for sqlDB plugin? You can find the file inside platforms/ios/www folder, also are you using cli or Xcode to build the project?

michal-th commented 8 years ago

Yes -

in module.exports:

{    
        "file": "plugins/cordova-plugin-dbcopy/www/sqlDB.js",
        "id": "cordova-plugin-dbcopy.sqlDB",
        "clobbers": [
            "window.plugins.sqlDB"
        ]
    },

and in metadata:

module.exports.metadata =
// TOP OF METADATA
{
    "cordova-plugin-console": "1.0.3",
    "cordova-plugin-dbcopy": "1.0.4",
    "cordova-plugin-device": "1.1.2",
    "cordova-plugin-splashscreen": "3.2.2",
    "cordova-plugin-statusbar": "2.1.3",
    "cordova-plugin-whitelist": "1.2.2",
    "cordova-sqlite-storage": "1.4.6",
    "ionic-plugin-keyboard": "2.2.1"
}
// BOTTOM OF METADATA
michal-th commented 8 years ago

haven't tried with Xcode yet, will do tonight and report back. My main issue is with Ionic View app, as that's the way client wants the preview to be delivered.

an-rahulpandey commented 8 years ago

Does it work properly when you run it as app directly without Ionic View? Also you can try wrapping the call like this -

document.addEventListener('deviceready', function() {
    window.plugins.sqlDB.copy("mydb.db",0, function () {
                alert('ok')
                console.log("copy ok")
            },function(e){
                alert(e)
                console.log("copy fail")
                console.log(e)
            });
});
michal-th commented 8 years ago

Wrapping in deviceready didn't help. I will try running it on the device directly from xcode and report back. Thanks for your help so far, much appreciated.

michal-th commented 8 years ago

It tried compiling directly from xcode, works without issues on both emulator and device (iPhone 5).

The only problem remains Ionic View app on both iPhone5 and iPad2, in both cases window.plugins do not contain sqlDB.

an-rahulpandey commented 8 years ago

May be this will help you -

https://github.com/driftyco/ionic-view-issues/issues/52#issuecomment-99250866

There are few workaround listed there.

michal-th commented 8 years ago

I tried this workaround, combined with the delay of up to 10 seconds - window.plugins still don't contain sqlDB. Is there possibly any other way how to access sqlDB?

It seems like this is more of a Ionic View issue..

Thanks again for your help!

michal-th commented 8 years ago

window.sqlitePlugin is set though, so it looks like it's loading that plugin.. maybe something directly related to window.plugins, as i can see it's set with other values but not sqlDB - probably gets overriden by ionic view?

an-rahulpandey commented 8 years ago

If you open the console in Safari developer tool and type window.plugins, what output does it shows? Also you can try one more thing, in the cordova_plugins.js change the window.plugins.sqlDB target to just sqlDB. And then in your js use just sqlDB.copy

michal-th commented 8 years ago

After 2 days of trying i gave up and wrote a workaround for ionic view - if plugin is not present i'm creating empty db and populate DB on the first run.

In all devices and emulators the plugin works flawlessly and without any issues - thank you for your work and i really appreciate your help!

Yurickh commented 8 years ago

This plugin is not supported by Ionic View as for this date. You can see the list of all supported plugins here: https://docs.ionic.io/tools/view/

amitpatil321 commented 7 years ago

I also faced this issue and the problem was DBcopy plugin wasnt installed properly, I reinstalled it and everything was fine.