user2828442
user2828442

Reputation: 2515

Ionic 4 with Sqlite - Cannot read property 'then' of undefined TypeError: Cannot read property 'then' of undefined

My database is not getting created. When I see my console, it shows the following error:

Uncaught (in promise): TypeError: Cannot read property 'then' of undefined TypeError: Cannot read property 'then' of undefined at new HomePage (home.page.ts:31)

I have installed npm install @ionic-native/sqlite@beta --save

Below is my home.page.ts code:

import { Component } from '@angular/core';
import { NavController } from '@ionic/angular';
import { SQLite, SQLiteObject } from '@ionic-native/sqlite/ngx';

@Component({
  selector: 'app-home',
  templateUrl: 'home.page.html',
  styleUrls: ['home.page.scss'],
})
export class HomePage {
constructor(public navCtrl: NavController,private sqlite: SQLite){
  this.sqlite.create({
    name: 'ionicdb.db',
    location: 'default'
  })
  .then((db: SQLiteObject) => {
    db.executeSql('CREATE TABLE IF NOT EXISTS expense2(rowid INTEGER PRIMARY KEY, date TEXT, type TEXT, description TEXT, amount INT)', [])
    .then(() => console.log('Executed SQL'))
    .catch(e => console.log(e, 'err'));
  })
  .catch(e => console.log(e)); 
}
}

I have done as per the documentation but dont know why this error is still there.

My project's ionic info -v for your reference:

ionic (Ionic CLI)             : 4.8.0 (C:\Users\jessica\AppData\Roaming\npm\node_modules\ionic)
   Ionic Framework               : @ionic/angular 4.0.0-rc.1
   @angular-devkit/build-angular : 0.11.4
   @angular-devkit/schematics    : 7.1.4
   @angular/cli                  : 7.1.4
   @ionic/angular-toolkit        : 1.2.2

Cordova:

   cordova (Cordova CLI) : 8.1.2 ([email protected])
   Cordova Platforms     : none
   Cordova Plugins       : no whitelisted plugins (2 plugins total)

System:

 (C:\Program Files (x86)\Android\android-sdk)
   NodeJS            : v10.15.0 (C:\Program Files\nodejs\node.exe)
   npm               : 6.0.1
   OS                : Windows 8.1

Upvotes: 2

Views: 3989

Answers (1)

Scott
Scott

Reputation: 827

I ran into this same issue. sqlite isn't available in browser naturally. So, if you just install the plugin, and do ionic serve, it's not going to work. Run these commands:

ionic cordova platform add browser

and then

ionic cordova run browser

I had to run the 2nd command twice. With the first command, it didn't build for me. After this, it worked.

Upvotes: 1

Related Questions