chrisgriffith / ionic-native-mocks

Ionic Native Mocks are designed to be used as placeholders during development for the actual Ionic Native modules.
205 stars 42 forks source link

SQLiteMock gives error #14

Closed remisture closed 6 years ago

remisture commented 6 years ago

[Error] ERROR – Error: Uncaught (in promise): TypeError: undefined is not an object (evaluating '_this.db.executeSql')

import { SQLiteMock } from '@ionic-native-mocks/sqlite';
providers: [
{ provide: SQLite, useClass: SQLiteMock },
]

Working code without mock class

import { Injectable } from '@angular/core';
import { Platform } from 'ionic-angular';
import { SQLite, SQLiteObject } from '@ionic-native/sqlite';

@Injectable()
export class DatabaseProvider {
    db: any;
    DB_VERSION: string = 'test_db';

    constructor(private sqlite: SQLite, private platform: Platform) {
        this.platform.ready().then(() => {
            this.openDatabase().then((db) => {
                this.db = db;
                this.db.query('SELECT * FROM cars').then((cars) => {
                   return cars.map((car) => {
                      return car.name;
                   });
               });
            });
        });
    }

    openDatabase() {
        return new Promise((resolve, reject) => {
            if (this.db) {
                resolve(this.db);
            }
            else {
                this.platform.ready().then(() => {

                    this.sqlite.create({ name: `${this.DB_VERSION}.db`, location: 'default', createFromLocation: 1 }).then((db: SQLiteObject) => {
                        this.db = db;
                        resolve(this.db);
                    }, (error) => {
                        reject(error);
                    });

                });
            }
        });
    }

    query(query = '', params = {}) {
        return this.openDatabase().then(() => {
            return this.db.executeSql(query, params).then((data) => {
                let rows = data.rows;
                let arr = [];

                for (let i = 0; i < rows.length; i++) {
                    arr.push(rows.item(i));
                }

                return arr;
            }).catch(e => {
                console.log(e);
                return [];
            });
        });
    }
}
Jordi-Carrasco commented 6 years ago

I have the same problem!

chrisgriffith commented 6 years ago

Sorry, I did not see this earlier. I will take a look. Thanks for supplying the sample code.

turrigni commented 6 years ago

I have the same problem this is my code:

import { Injectable } from '@angular/core';
import { Http } from '@angular/http';
import 'rxjs/add/operator/map';

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: Http,
    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 users (id INTEGER PRIMARY KEY AUTOINCREMENT, identification INTEGER, name TEXT, lastname text)", []);
        this.isOpen = true;
        console.log("DatabaseProvider db open");
      }).catch((error) => {
        console.error("DatabaseProvider db open fail");
        console.log(error);
      })
    }
  }

  CreateUser(identification: number, name:string, lastname:string){
    return new Promise ((resolve, reject) => {
      let sql = "INSERT INTO users (identification, name, lastname) VALUES (?, ?, ?)";
      this.db.executeSql(sql, [identification, name, lastname]).then((data) =>{
        resolve(data);
      }, (error) => {
        reject(error);
      });
    });
  }

  GetAllUsers(){
    return new Promise ((resolve, reject) => {
      this.db.executeSql("SELECT * FROM users", []).then((data) => {
        let arrayUsers = [];
        if (data.rows.length > 0) {
          for (var i = 0; i < data.rows.length; i++) {
            arrayUsers.push({
              id: data.rows.item(i).id,
              identification: data.rows.item(i).identification,
              name: data.rows.item(i).name,
              lastname: data.rows.item(i).lastname
            });            
          }          
        }
        resolve(arrayUsers);
      }, (error) => {
        reject(error);
      })
    })
  }

}

the problem is on creation of db, i see this log:

DatabaseProvider db open fail cordova_not_available

chrisgriffith commented 6 years ago

When I created this mock, it was a simple port of the method calls. Because this is a complex plugin, involving db access, it is hard to write a functioning mock that will replicate the suite of abilities. I am afraid this is one plugin that you will not be able to use mocks for.