TypeError: Cannot read property 'executeSql' of undefined

Hi Team,
Getting TypeError: Cannot read property ‘executeSql’ of undefined error when trying to connect and insert into sqllite data base. ionic Version is 4.1.1. Code and Error below. Almost 3 days to resolve this. Kindly Guide.

Code:-
import { HttpClient } from ‘@angular/common/http’;
import { Injectable } from ‘@angular/core’;

import { SQLite, SQLiteObject } from ‘@ionic-native/sqlite’;

/*
Generated class for the DatabaseProvider provider.

See https://angular.io/guide/dependency-injection for more info on providers
and Angular DI.
*/

@Injectable()
export class DatabaseProvider {
private db: SQLiteObject;
private isOpen : boolean;

constructor(
public http: HttpClient,
public storage: SQLite
) {

if (!this.isOpen) {
this.storage = new SQLite();
this.storage.create({name:“data.db”, location:“default”}).then((db:SQLiteObject) => {
this.db = db;
db.executeSql(“CREATE TABLE IF NOT EXISTS yes_master(master_id INTEGER, address TEXT)”,[]);
this.isOpen = true;
}).catch((error) => {
console.log(error);
})
}
}

CreateFamily(master_id:number,address:string){
return new Promise ((resolve, reject) => {
let sql = “INSERT INTO yes_master (master_id,address) VALUES (?,?)”;
this.db.executeSql(sql, [master_id,address]).then((data) =>{
resolve(data);
}, (error) => {
reject(error);
});
});
}

GetAllFamily(){
return new Promise ((resolve, reject) => {
this.db.executeSql(“SELECT * FROM family_master”,[]).then((data) =>{
let arrayFamily =[];
if (data.rows.length > 0) {
for (var i =0; i < data.rows.length; i++) {
arrayFamily.push({
master_id: data.rows.item(i).master_id,
address: data.rows.item(i).address
});
}
}
resolve(arrayFamily);
}, (error) => {
reject(error);
})
})
}
}

Error Getting:-
TypeError: Cannot read property ‘executeSql’ of undefined
at database.ts:39
at new t (polyfills.js:3)
at DatabaseProvider.webpackJsonp.197.DatabaseProvider.CreateFamily (database.ts:37)
at HomePage.webpackJsonp.196.HomePage.CreateFamily (home.ts:14)
at Object.eval [as handleEvent] (HomePage.html:10)
at handleEvent (core.js:13589)
at callWithDebugContext (core.js:15098)
at Object.debugHandleEvent [as handleEvent] (core.js:14685)
at dispatchEvent (core.js:10004)
at core.js:10629

Replace private isOpen : boolean; with private isOpen : boolean = false and test

1 Like

No, don’t explicitly instantiate SQLite objects at all. Simply use the one that is injected into your constructor.

1 Like

Thank You Bavis, Rapropos. figured it out, a browser cannot load sqlite was the problem. after loading the same in Android simulator it working good. I am rookie and learning things. Thank You all for your support

1 Like

I am also getting error in ionic 4 in mobile device executeSql of undefined. Can any one share solution.

Evaluate every place you call executeSql and ensure that the object you’re doing so on is defined. Generally errors like that also have a line number to help you pinpoint the particular one that is causing problems, but doing a more general audit will help prevent further such problems in the future.