TypeError: Cannot read property 'then' of undefined while using ionic-v4 with Sqlite

#1

Hi,

I am a newbie in ionic. I am trying to create a simple app with ionic + sqlite db.
But, every time I run I am getting the following error.

core.js:15714 ERROR Error: Uncaught (in promise): TypeError: Cannot read property 'then' of undefined
TypeError: Cannot read property 'then' of undefined

here is the app.component.ts

import { Component } from '@angular/core';

import { Platform } from '@ionic/angular';
import { SplashScreen } from '@ionic-native/splash-screen/ngx';
import { StatusBar } from '@ionic-native/status-bar/ngx';
import { SQLite, SQLiteObject } from '@ionic-native/sqlite/ngx';

@Component({
  selector: 'app-root',
  templateUrl: 'app.component.html'
})
export class AppComponent {

   private db: SQLiteObject;

  constructor(
    private platform: Platform,
    private splashScreen: SplashScreen,
    private statusBar: StatusBar, 
    private sqlite: SQLite

  ) {
    this.initializeApp();
  }

  initializeApp() {
    this.platform.ready().then(() => {
      this.createDB();
      this.statusBar.styleDefault();
      this.splashScreen.hide();
    });
  }

  private createDB() {
     alert('database creating')
    this.sqlite.create({
      name: 'myDB.db',
      location: 'default'
    })
    .then((db: SQLiteObject) => {
      alert('database created')
      this.db = db;
      // this.createTable();
    })
    .catch(e => alert(e));
  }
}

can anybody help me to solve this.

Regards

#2

I also have the same problem, so far I still can’t fix it.

#3

If you are running in a browser (ionic serve) , look in the Console. You will probably see thiis just before your error:

Native: tried accessing the SQLite plugin but Cordova is not available. Make sure to include cordova.js or run in a device/simulator

SQlite is a Cordova plugin and Cordova must be avalable

#4

Thanks for the explanation. That helped :slight_smile: