Help with Ionic Storage


#1

Hello guys, please i need help with ionic storage. I have a few questions

  1. When i save data to storage should i stringify it first if it is an array?

  2. I have a service which perform my crud operations for me. I want to return the data saved in my storage through a service to my home page but i get an error which says void is not assignable to type Promise<Todo>

Below is some codes from my service page for more clarification

 private todos: Todo[] = [];

getAllTodos(){
    this.storage.get(STORAGE_KEY).then((result) => {
      this.todos = result == null ? [] : result;
  	return [...this.todos];
    });
  }
  1. On my home page, i have a code that looks like
private todos: Promise<Todo[]>;
  private todo: Todo;

  ionViewWillEnter(){
    this.todos = this.getAllTodo(); //This is the line that says  void is not assignable to type Promise<Todo>
  }

getAllTodo(){
    return this.todoService.getAllTodos();
  }

Please i’d appreciate it so much if anyone is able to help me understand this concept.


#2

Not necessary.

Not possible in the way that you are attempting. Turn on noImplicitAny in your tsconfig.json and the build process will yell at you when you fail to declare return value types of any function. You would then probably try to declare getAllTodos() as returning Todo[] (which it can’t do, and the build tools will guide you through catching and noticing this). The best you can do is to return a Promise<Todo[]>, and then hang whatever needs to be done in whoever is calling getAllTodos() off of a then clause.

You cannot synchronously return the result of an asynchronous operation without access to a time machine.


#3

Thanks for the explanation. I will try it out