Josua Stricker
Josua Stricker

Reputation: 1

Type 'void' is not assignable to type 'SQLiteDBConnection | null'

I am in the process of developing an app with ionic/React + sqlite.

At the moment I have a problem initializing the SQLite connection.

I get the following error in the following two lines:

Type 'void' is not assignable to type 'SQLiteDBConnection | null'.ts(2322) this: this

this.db = await CapacitorSQLite.createConnection({

Object is possibly 'null'.ts(2531) this: this

await this.db.open();

Does anyone have an idea what is wrong here and how I can correct it?

import { Capacitor } from '@capacitor/core';
import { CapacitorSQLite, SQLiteDBConnection, SQLiteConnection } from '@capacitor-community/sqlite';

export class SQLiteService {
  private db: SQLiteDBConnection | null = null;

  // Asynchrone Methode zum Initialisieren der Datenbankverbindung
  private async initDbConnection(): Promise<boolean> {
    try {
      // Versuche, eine Verbindung zur SQLite-Datenbank herzustellen
      this.db = await CapacitorSQLite.createConnection({
        database: 'wood_list',     // Name der Datenbank
        version: 1,                // Version der Datenbank
        encrypted: false,          // Unverschlüsselte Datenbank
        mode: 'no-encryption',     // Kein Verschlüsselungsmodus       
      });

      // Öffne die Datenbankverbindung
      await this.db.open();

      // Erfolgslog: Datenbankverbindung erfolgreich hergestellt
      console.log('Datenbankverbindung erfolgreich hergestellt.');

      // Rückgabe true, wenn die Verbindung erfolgreich ist
      return true;
    } catch (error) {
      // Fehlerbehandlung: Ausgabe der Fehlermeldung
      console.error('Fehler bei der Datenbankverbindung:', error);

      // Rückgabe false, wenn ein Fehler auftritt
      return false;
    }
  }
}

Upvotes: 0

Views: 18

Answers (0)

Related Questions