1

I am trying to create an Ionic 4 application, and using SQLite as storage. I've installed the following packages:

ionic cordova plugin add cordova-sqlite-storage
npm install --save @ionic-native/sqlite
ionic cordova plugin add uk.co.workingedge.cordova.plugin.sqliteporter
npm install --save @ionic-native/sqlite-porter

Here is my app.module:

@NgModule({
  declarations: [AppComponent],
  entryComponents: [],
  imports: [BrowserModule, IonicModule.forRoot(), AppRoutingModule, 
HttpClientModule],
  providers: [
    StatusBar,
    SplashScreen,
    { provide: RouteReuseStrategy, useClass: IonicRouteStrategy,  },
    SQLite,
    SQLitePorter,
    DatabaseService
  ],
  bootstrap: [AppComponent]
})

Here is the section of my DatabaseService where the code throws an error, it fails on the .create and returning undefined:

initializeDatabase(): void {
    console.log("initializing db");
    // Define the application SQLite database
    this._SQL
      .create({
        name: this._DB_NAME,
        location: "default"
      })
      .then((db: SQLiteObject) => {
        // Associate the database handler object with the _DB private property
        this._DB = db;
        console.log("db created");
      })
      .catch(e => {
        console.log(e);
      });
}

Here is my error: TypeError: Cannot read property 'then' of undefined at DatabaseService.push../src/app/services/database.service.ts.DatabaseService.initializeDatabase (database.service.ts:49) at new DatabaseService (database.service.ts:33) at _createClass (core.js:21263) at _createProviderInstance (core.js:21225) at resolveNgModuleDep (core.js:21189) at NgModuleRef_.push../node_modules/@angular/core/fesm5/core.js.NgModuleRef_.get (core.js:21897) at resolveDep (core.js:22268) at createClass (core.js:22148) at createDirectiveInstance (core.js:22019) at createViewNodes (core.js:23245)

Please help, I've been stuck on this for two days.

Sksdow Psn
  • 19
  • 3
  • I am just testing my application on the browser using Ionic serve – Sksdow Psn Feb 25 '19 at 00:21
  • try testing you application with this command ionic cordova run browser -l – Seloka Feb 25 '19 at 06:29
  • I am still getting the same error with that command `AppComponent_Host.ngfactory.js? [sm]:1 ERROR TypeError: Cannot read property 'then' of undefined at DatabaseService.push../src/app/services/database.service.ts.DatabaseService.initializeDatabase (database.service.ts:49) at new DatabaseService (database.service.ts:33) ...` – Sksdow Psn Feb 25 '19 at 14:34
  • are you getting the following warnings from your dev tools? 1. Native: tried accessing the SQLite plugin but it's not installed 2. Install the SQLite plugin: 'ionic cordova plugin add cordova-sqlite-storage if so, try testing you app on an actual device, and it will work. Some cordova plugins are intended for use in a native mobile environment. they will NOT work in a browser-emulated Cordova environment, for example by running cordova serve – Seloka Feb 25 '19 at 14:51

1 Answers1

0

It throws error if you are testing it in the browser because Cordova will run only in the mobile.You have to test the application either in a mobile or emulator

Cordova will run only on the mobile platforms such as Android and Ios

Sivaramakrishnan
  • 689
  • 1
  • 4
  • 11