Service call to TS file gets Undefined

This is my Service

input: string;

constructor(private storage: Storage,
  public settingsModel: SettingsModel,
) { }

getData(key) {
  this.storage.get(key).then((val) => {
    this.input = val;
    console.log('Storage value: ' + this.input);
  });
}

setData(key: string, value: string) {
  this.storage.set(key, value);
}

TS file.`input: string;

constructor(
  public navCtrl: NavController,
  public settingsFlowService: SettingsFlowService
) {}

ngOnInit() {
  this.settingsFlowService.input = this.input;
  console.log(this.input)
}

When I log From Service displays what i have in storage, which is fine When i log in TS file gets undefined any help ?

1 answer

  • answered 2018-10-17 08:02 rrd

    Well you aren't using your service's method. I assume your service is the 'settingsFlowService'. You need to call the 'getData()' method with the key you stored your data with (ie what you originally call 'setData()' with) which you missed out so I'm just guessing at one. If you did this it would probably work:

    ngOnInit() {
      this.input = this.settingsFlowService.getData('somekey');
      console.log(this.input)
    }