Samir Danial
Samir Danial

Reputation: 79

IONIC 4 + Angular7: ERROR Error: Uncaught (in promise): TypeError: Cannot read property 'then' of undefined

I want to assign the result of creating SQLite database to variable database of type SQLiteObject for this reason I have to do this in "then" block but it shows

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

I have tried most of the articles about this error "ERROR Error: Uncaught (in promise): TypeError: Cannot read property 'then' of undefined" but none of them is my answer

constructor(public http: Http,
    private sqlitePorter: SQLitePorter,
    private storage: Storage,
    private sqlite: SQLite,
    private plateform: Platform) {
    this.databaseReady = new BehaviorSubject(false);
    this.plateform.ready().then(() => {
      this.sqlite.create({
        name: 'developers.db',
        location: 'default',
        })
        .then((db: SQLiteObject) => {
        this.database = db;
        this.storage.get('database_filled').then(val => {
          if (val) {
            this.databaseReady.next(true);
          } else {
            this.fillDatabase();
          }
        })
      })
    })
  }

This is the actual code

this is the error

Upvotes: 0

Views: 2124

Answers (1)

Kushagra Saxena
Kushagra Saxena

Reputation: 669

You will have to return a promise from the first then. As you are not returning anything from the first then it will return undefined. Try something like this.

this.plateform.ready().then(() => {
      return this.sqlite.create({
        name: 'developers.db',
        location: 'default',
        })
        .then((db: SQLiteObject) => {
        this.database = db;
        this.storage.get('database_filled').then(val => {
          if (val) {
            this.databaseReady.next(true);
          } else {
            this.fillDatabase();
          }
        })
      })
    })

Upvotes: 1

Related Questions