0

Below are my imports in my database.service.ts.

import { Platform } from '@ionic/angular';
import { Injectable } from '@angular/core';
import { SQLitePorter } from '@ionic-native/sqlite-porter/ngx';
import { HttpClient } from '@angular/common/http';
import { SQLite, SQLiteObject } from '@ionic-native/sqlite/ngx';
import { BehaviorSubject, Observable } from 'rxjs';

Below is the constructor I am using to try to build the database.

  constructor(private plt: Platform, private sqlitePorter: SQLitePorter, private sqlite: SQLite, private http: HttpClient) {
    this.plt.ready().then(() => {
      const conn = this.sqlite.create({
        name: 'emotions.db',
        location: 'default'
      });
      if (conn == null) throw Error('Failed to create database connection');
      conn.then(() => {
        console.log('Done');
      })
    });
  }

This is run when the app begins, and throws the statement I told it to because the conn == null in this case.

I am unsure why this is occurring, as there are no build errors when doing 'ionic serve'.

1 Answer 1

1

This is the wrong way to get a response, just try the way of ionic Documentation,

this.sqlite.create({
  name: 'data.db',
  location: 'default'
})
  .then((db: SQLiteObject) => {
    console.log(db);
  })
  .catch(e => console.log(e));
Sign up to request clarification or add additional context in comments.

10 Comments

Unfortunately, this still returns the same error. I am unsure why, but I don't know if the plugin hasn't been installed properly, but since there are no build errors this seems unlikely.
I suggest you remove and reinstall the plugin again.
I am using the same thing in my project and it's working absolutely fine.
as I am seeing you are using service for this, am I right?
I have done these commands: ionic cordova plugin add cordova-sqlite-storage npm install @ionic-native/sqlite After this, it is still not working.
|

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.