ionic-team / ionic-storage

Ionic Storage module for Ionic apps
MIT License
434 stars 99 forks source link

Error: No available storage method found when using CordovaSQLiteDriver with Ionic Storage #298

Open wynlo opened 1 year ago

wynlo commented 1 year ago

Hi, I want to move from IndexedDB to SQLite and I am currently trying out the SQLite installation instructions. I've created a StorageService that specifies a custom driver implementation and I am trying to run my Ionic application in the browser.

When I include Drivers.IndexedDB in the driverOrder list even though CordovaSQLiteDriver is placed first and defined, Ionic Storage defaults to IndexedD and ignores CordovaSQLiteDriver.

When I comment out IndexedDB, the following exception is raised:

Error: Uncaught (in promise): Error: No available storage method found.
Error: No available storage method found.
    at vendor.js:31073:25 [angular]
    at Object.onInvoke (core.mjs:26108:33) [angular]
    at polyfills.js:9046:28 [angular]
    at vendor.js:74280:49 [angular]
    at AsyncStackTaggingZoneSpec.onInvokeTask (core.mjs:25794:36) [angular]
    at Object.onInvokeTask (core.mjs:26095:33) [angular]
    at drainMicroTaskQueue (zone.js:581:35) [<root>]
    at resolvePromise (zone.js:1193:31) [angular]
    at polyfills.js:8895:9 [angular]
    at polyfills.js:8911:25 [angular]
    at asyncGeneratorStep (asyncToGenerator.js:6:1) [angular]
    at _throw (asyncToGenerator.js:25:1) [angular]
    at Object.onInvoke (core.mjs:26108:33) [angular]
    at polyfills.js:9046:28 [angular]
    at vendor.js:74280:49 [angular]
    at AsyncStackTaggingZoneSpec.onInvokeTask (core.mjs:25794:36) [angular]

The same issue happens when I am testing in an iOS simulator.

Would really appreciate any advice on how to resolve this issue or if I am missing out on something. Am I supposed to instantiate SQLite somewhere else first?

Here are the files that I am using:

app.module.ts

@NgModule({
    declarations: [AppComponent],
    imports: [
        BrowserModule,
        HttpClientModule,
        IonicModule.forRoot(),
        IonicStorageModule.forRoot({
            driverOrder: [
                CordovaSQLiteDriver._driver,
                // Drivers.IndexedDB
            ]
        }),
        AppRoutingModule,
        DirectivesModule,
    ],
    providers: [
        StatusBar,
        BackgroundMode,
        DatePicker,
        SocialSharing,
        Clipboard,
        LocalNotifications,
        GoogleAnalytics,
        File,
        { provide: RouteReuseStrategy, useClass: IonicRouteStrategy },
        { provide: ErrorHandler, useClass: GlobalErrorHandlerService },
    ],
    bootstrap: [AppComponent]
})
export class AppModule { }

storage.service.ts

import { Injectable } from '@angular/core';
import { Storage, Drivers } from '@ionic/storage';
import CordovaSQLiteDriver from 'localforage-cordovasqlitedriver'

@Injectable({
  providedIn: 'root'
})
export class StorageService {

  private _storage: Storage | null = null;

  constructor() { }

  async init(): Promise<void> {
    await this.storage.defineDriver(CordovaSQLiteDriver);
    console.log(this.storage.driver); // null
    this._storage = await this.storage.create();
    console.info(`STORAGE DRIVER : ${storage.driver}`);
  }

  public get(key: string): Promise<any> {
    return this._storage.get(key);
  }

  public set(key: string, value: any): Promise<any> {
    return this._storage.set(key, value);
  }

  public getStorage(): Storage {
    return this._storage;
  }

}
puddinator commented 1 year ago

I just realized after many many hours that it doesn't work on web or even android studios emulator - but it does work on real device

Maybe this can be included in the docs, but it is also not really Ionic Storage's issue