Can't get value from storage after push page

my storage provider

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

@Injectable()
export class TestStorageProvider {

  constructor(public storage: Storage) {
    storage.ready().then(() => { });
  }

  save() {
    this.storage.ready().then(() => {
      this.storage.set('name', 'Max5');
    });
  }

 load(): any {
    this.storage.ready().then(() => {
      this.storage.get('name').then((val) => {
        console.log('Your name is', val);
      });
    });
  }
}

page 1

import { Component } from '@angular/core';
import { IonicPage, NavController, NavParams } from 'ionic-angular';
import { TestStorageProvider } from '../../providers/test-storage/test-storage';

@IonicPage()
@Component({
  selector: 'page-summary',
  templateUrl: 'summary.html',
})
export class Summary {

  constructor(
    public navCtrl: NavController,
    public navParams: NavParams,
    private testStorage: TestStorageProvider,
  ) {
  }

  ionViewDidLoad() { }

  goToTest1() {
    this.testStorage.save();
    this.navCtrl.push('Test1Page');
  }

}

page 2

import { Component } from '@angular/core';
import { IonicPage, NavController, NavParams } from 'ionic-angular';
import { TestStorageProvider } from '../../providers/test-storage/test-storage';

@IonicPage()
@Component({
  selector: 'page-test1',
  templateUrl: 'test1.html',
})
export class Test1Page {

  constructor(
    public navCtrl: NavController,
    public navParams: NavParams,
    private testStorage: TestStorageProvider,
  ) {
  }

  ionViewDidLoad() {
    this.testStorage.load();
  }

}

Did i have to set timeout to wait before get value ?

  • TestStorageProvider's constructor is pointless.
  • No, you never need to use setTimeout.
  • load() doesn’t do or return anything.
  • stop abusing any.
load(): Promise<string> {
  return this.storage.ready().then(() => this.storage.get('name'));
}

Test1Page {
  name: string;
  constructor(tsp: TestStorageProvider) {
    tsp.load().then(name => this.name = name);
  }
}

I’m also not convinced that you really want to be using Storage here. It’s only needed if you want this name property to persist across app restarts.

1 Like

I’m plan to use storage for keep data like username, password, cart and stuck with this.
Because after i push to another page value that i got isn’t new value i set. On first time i set value then load it i got null. So i think i have to use timeout to wait it to get new value.

it still not work.

No, you don’t.

Under no circumstances store cleartext passwords in storage. That is asking to get sued.

Please post enough code that somebody trying to help you can reproduce this.

i move it to github

Current behavior
1st time : input aaa got null
2nd time : input bbb got aaa
Expected behavior
1st time : input aaa got aaa
2nd time : input bbb got bbb

This really highlights why storage is a bad fit for what you’re trying to do, but if you really insist on continuing down this road, you are responsible for ensuring the write has completed before you try to read:

toAbout(): void {
  this.storage.save(this.name).then(() => this.navCtrl.push('AboutPage'))
}
1 Like

Thanks. It’s work.:smile: