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

i getting core.js:6014 ERROR Error: Uncaught (in promise): TypeError: Cannot read property ‘then’ of undefined
TypeError: Cannot read property ‘then’ of undefined error while using SQlite in latest ionic version.

please solve my question

thanks in advance

Please post the Code Piece, where the Error appears.

Database.service.ts

import { Injectable } from ‘@angular/core’;
import { Platform } from ‘@ionic/angular’;
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’;

export interface DriversList {
id: number,
name: string,
skills: any,
img: string
}

@Injectable({
providedIn: ‘root’
})
export class DatabaseService {
private database: SQLiteObject;
private dbReady: BehaviorSubject = new BehaviorSubject(false);

driver = new BehaviorSubject();
// products = new BehaviorSubject();

constructor(private plt: Platform, private sqlitePorter: SQLitePorter, private sqlite: SQLite, private http: HttpClient) {
this.plt.ready().then(() => {
this.sqlite.create({
name: ‘driver.db’,
location: ‘default’
})
.then((db: SQLiteObject) => {
** console.log(‘success creationDB’);**

** this.database = db;**
** this.driversDatabase();**
});
});
}

driversDatabase() {
this.http.get(’…/assets/driversListData.sql’, { responseType: ‘text’ })
.subscribe(sql => {
this.sqlitePorter.importSqlToDb(this.database, sql)
.then(_ => {
console.log(‘success drversdatabase’);
this.loadDevelopers();
// this.loadProducts();
this.dbReady.next(true);
})
.catch(e => console.error(e));
});
}

getDatabaseState() {
return this.dbReady.asObservable();
}

getDevs(): Observable<DriversList> {
return this.driver.asObservable();
}

// getProducts(): Observable<any> {
// return this.products.asObservable();
// }

loadDevelopers() {
console.log(‘success load driver’);

return this.database.executeSql('SELECT * FROM driver', []).then(data => {
  const drivers: DriversList[] = [];
  console.log('data driver', data);
  

  if (data.rows.length > 0) {
    for (var i = 0; i < data.rows.length; i++) {
      let skills = [];
      if (data.rows.item(i).skills != '') {
        skills = JSON.parse(data.rows.item(i).skills);
      }

      drivers.push({
        id: data.rows.item(i).id,
        name: data.rows.item(i).name,
        skills: skills,
        img: data.rows.item(i).img
      });
    }
  }
  this.driver.next(drivers);
});

}

}

that highlilghted code is getting error

Try reading this thread.

Incidentally, you don’t need asObservable.

getDevs(): Observable<DriversList[]> {
  return this.driver;
}

…should do the same thing just fine.

Hi raprops,

getting same error :slightly_frowning_face:

ERROR Error: Uncaught (in promise): TypeError: Cannot read property ‘then’ of undefined
TypeError: Cannot read property ‘then’ of undefinedPreformatted text

Note** error is getting for the constructor code db ready
my updated code

// db.service.ts

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

@Injectable({
  providedIn: 'root'
})

export class DbService {
  private storage: SQLiteObject;
  songsList = new BehaviorSubject([]);
  private isDbReady: BehaviorSubject<boolean> = new BehaviorSubject(false);

  constructor(
    private platform: Platform, 
    private sqlite: SQLite, 
    private httpClient: HttpClient,
    private sqlPorter: SQLitePorter,
  ) {
    this.platform.ready().then(() => {
      this.sqlite.create({
        name: 'positronx_db.db',
        location: 'default'
      })
      .then((db: SQLiteObject) => {
        this.storage = db;
         this.getFakeData();
      });
    });
  }

  dbState() {
    return this.isDbReady;
  }
 
  fetchSongs(): Observable<Song[]> {
    return this.songsList;
  }

    // Render fake data
    getFakeData() {
      this.httpClient.get(
        'assets/dump.sql', 
        {responseType: 'text'}
      ).subscribe(data => {
        this.sqlPorter.importSqlToDb(this.storage, data)
          .then(_ => {
            this.getSongs();
            this.isDbReady.next(true);
          })
          .catch(error => console.error(error));
      });
    }

  // Get list
  getSongs(){
    return this.storage.executeSql('SELECT * FROM songtable', []).then(res => {
      let items: Song[] = [];
      if (res.rows.length > 0) {
        for (var i = 0; i < res.rows.length; i++) { 
          items.push({ 
            id: res.rows.item(i).id,
            artist_name: res.rows.item(i).artist_name,  
            song_name: res.rows.item(i).song_name
           });
        }
      }
      this.songsList.next(items);
    });
  }

  // Add
  addSong(artist_name, song_name) {
    let data = [artist_name, song_name];
    return this.storage.executeSql('INSERT INTO songtable (artist_name, song_name) VALUES (?, ?)', data)
    .then(res => {
      this.getSongs();
    });
  }
 
  // Get single object
  getSong(id): Promise<Song> {
    return this.storage.executeSql('SELECT * FROM songtable WHERE id = ?', [id]).then(res => { 
      return {
        id: res.rows.item(0).id,
        artist_name: res.rows.item(0).artist_name,  
        song_name: res.rows.item(0).song_name
      }
    });
  }

  // Update
  updateSong(id, song: Song) {
    let data = [song.artist_name, song.song_name];
    return this.storage.executeSql(`UPDATE songtable SET artist_name = ?, song_name = ? WHERE id = ${id}`, data)
    .then(data => {
      this.getSongs();
    })
  }

  // Delete
  deleteSong(id) {
    return this.storage.executeSql('DELETE FROM songtable WHERE id = ?', [id])
    .then(_ => {
      this.getSongs();
    });
  }
}