Hacktoberfest 2026: los issues que los mantenedores marcaron para octubre, abiertos y aptos para principiantes. Explorar issues de Hacktoberfest

service from documentation creates async bug at startup

Abierto
#229 7 comentarios 42 reacciones 0 asignados Ver en GitHub

Nadie ha tomado este issue todavía.

Evaluación

Dificultad
2/5
Tiempo estimado
1-3 horas
Aptitud para principiantes
42/100
Tipo de issue
Error
Claridad
Bien especificado
Estado de actividad
Estancado
Stack tecnológico
angular, typescript

Línea de trabajo

Comienza con el ejemplo del servicio de almacenamiento de Angular en README.md y compara la inicialización de su constructor con la del servicio mostrado en el issue. Actualiza la documentación para que las lecturas tempranas del almacenamiento esperen a la inicialización y, después, verifica que el ejemplo ya no devuelva undefined cuando se accede al almacenamiento durante el inicio de la app.

Escrito por el modelo de indexación a partir del texto del issue.

Descripción

From my testing of the example service in the documentation, it seems that it contains a bug.

The constructor calls init() without async (because it's not supported in a constructor):

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

However, that means that if your app is relying on the storage very early on, you are going to get back undefined from a get.

For me, I have a tutorial guard set up which checks if the tutorial has been completed to decide if it should show the homepage or redirect to the tutorial slider page. It was always showing the tutorial because the result was undefined.

I've improved the service so that it takes a similar approach to the way that Ionic Storage is actually written; which is checking if the storage is initialised before trying to operate on it.

That way it can await the proper completion of the storage:

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

import * as CordovaSQLiteDriver from 'localforage-cordovasqlitedriver';

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

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

  constructor(private storage: Storage) {
  }

  async init() {
    if(this._storage != null) {
      return;
    }
    await this.storage.defineDriver(CordovaSQLiteDriver);
    const storage = await this.storage.create();
    this._storage = storage;
  }

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

  public async get(key: string): Promise<any> {
    await this.init();
    return await this._storage?.get(key);
  }
}
Lenguaje dominante
TypeScript
Estrellas
456
Forks
97
Métricas de merge de PR
Sin PR fusionados en 30 d

Preparar el entorno

Primeros pasos

  1. Lee el issue completo y luego la guía de contribución del proyecto.
  2. Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
  3. Haz un fork del repositorio y trabaja en una rama.
  4. Abre un pull request que haga referencia al número del issue.

Más de ionic-team/ionic-storage

Todos los issues de ionic-team/ionic-storage

Issues similares

Más issues de TypeScript

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.