Wait for executeSql in expo-sqlite

Viewed 202

I have a database class called DBTasks. Whenever I call DBTasks.getAll(), it will always return nothing, because the sqlite API is asynchronous, when I don't want it to be. There also might be a scope problem.

// Import SQLite
import * as SQLite from 'expo-sqlite';

export default class DBTasks {
    constructor() {
        this.dbName = 'coolApp';
        this.tableName = 'tasks';
        this.db = SQLite.openDatabase(this.dbName + '.db');
        
        this.create();
    }
    
    create() {
        this.db.transaction(tx => {
            tx.executeSql(
                'CREATE TABLE IF NOT EXISTS ' + this.tableName + ' (id INTEGER PRIMARY KEY, title TEXT, timeDue INTEGER, categoryID INTEGER, canvasID TEXT );'
            );
        });
    }
    
    getAll() {
        this.db.transaction(tx => {
            tx.executeSql('SELECT * FROM ' + this.tableName + ';', [], (tx, results) => {
                const rows = results.rows._array;
                return rows;
            });
        });
    }
}

0 Answers
Related