2

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
10
  • Have you added SQLite plugin in your APP? refer this:ionicframework.com/docs/native/sqlite Commented Jan 20, 2019 at 8:56
  • I have mentioned that i have already installed it Commented Jan 20, 2019 at 9:43
  • 1
    I am talking about native SQLite plugin: ionic cordova plugin add cordova-sqlite-storage Commented Jan 20, 2019 at 9:46
  • I have added this, is it ok or i need to add some other? - ionic cordova plugin add cordova-sqlite-storage Commented Jan 20, 2019 at 9:51
  • If you added these 2 then make sure you are calling it after platform.ready event and running the APP in emulator or physical device. Commented Jan 20, 2019 at 9:59

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

this command give error like: Using cordova-fetch for cordova-browser@^6.0.0 Platform browser already added. [ERROR] An error occurred while running subprocess cordova. cordova platform add browser exited with exit code 1.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.