Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ERROR Error: Uncaught (in promise): NullInjectorError: R3InjectorError(AppModule)[AuthGuard -> StorageService -> Storage -> Storage]: NullInjectorError: No provider for Storage! #308

Open
iamSwaps opened this issue Jul 24, 2023 · 2 comments

Comments

@iamSwaps
Copy link

hey i'm facing this error

ERROR Error: Uncaught (in promise): NullInjectorError: R3InjectorError(AppModule)[AuthGuard -> StorageService -> Storage -> Storage]: 
  NullInjectorError: No provider for Storage!
NullInjectorError: R3InjectorError(AppModule)[AuthGuard -> StorageService -> Storage -> Storage]: 
  NullInjectorError: No provider for Storage!
    at NullInjector.get (core.mjs:8771:27)
    at R3Injector.get (core.mjs:9200:33)
    at R3Injector.get (core.mjs:9200:33)
    at injectInjectorOnly (core.mjs:652:33)

here is the service i have created

import { Injectable } from '@angular/core';

import { Storage } from '@ionic/storage-angular';

@Injectable({
  providedIn: 'root'
})
export class StorageService {
  private _storage: Storage;

  constructor(private storage: Storage) {
    this.init();
  }

  async init() {
    // If using, define drivers here: await this.storage.defineDriver(/*...*/);
    const storage = await this.storage.create();
    this._storage = storage;
    this._storage.set('isAdmin',false);
    this._storage.set('token','');
  }

  // Create and expose methods that users of this service can
  // call, for example:
  public set(key: string, value: any) {
    this._storage?.set(key, value);
  }

  public get(key: string){
    return this._storage?.get(key);
  }
}
@HugoLeBoennec
Copy link

You need to register all the services you created and the one you are using in your main.ts file if you are using Standalone components

example:

...
import { Storage } from '@ionic/storage-angular';
import { YourService } from './app/services/your.service';

...
bootstrapApplication(AppComponent, {
  providers: [
    { provide: RouteReuseStrategy, useClass: IonicRouteStrategy },
    importProvidersFrom(IonicModule.forRoot({})),
    provideRouter(routes),
    provideHttpClient(),
    YourService, // <-- Here
    Storage  // <-- Here
  ],
});

@vespina
Copy link

vespina commented Oct 1, 2023

This solved the problem for me. It would be nice that you update the main README.MD so this is included in the main documentation, so people doesn't fall on this every time.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants