1

I'm currently facing an issue, when the UserData is injected into my other component, the Load() function will always started earlier than the getFromStorageAsync() within the constructor, so ip_address is always undefined.

the below is my service code (UserData)

import { Injectable } from '@angular/core';
import { Http } from '@angular/http';
import { Storage } from '@ionic/storage';
import 'rxjs/add/operator/map';

/*
  Generated class for the UserData provider.

  See https://angular.io/docs/ts/latest/guide/dependency-injection.html
  for more info on providers and Angular 2 DI.
*/
@Injectable()
export class UserData {
  public data: any;
  private ip_address: any;

  constructor(public http: Http, private storage: Storage) {
    this.data = null;
    this.ip_address = this.getFromStorageAsync()
  }

  async getFromStorageAsync(){
        let val = await this.storage.get('ip_address');
        this.ip_address = val.toPromise();
    }

  load(): any {
    console.log(this.ip_address);
    if (this.data) {
      return Promise.resolve(this.data);
    }
    return new Promise(resolve => {
      this.http.get(this.ip_address + "/api/user")
        .map(res => res.json())
        .subscribe(data => {
          this.data = data;
          resolve(this.data);
        });
    });
  }
}

it failed to fetch data from server due to undefined ip address. is it correct way to use async and await. please help. thanks !

2
  • Where you call load() method? Commented Apr 28, 2017 at 21:19
  • in another component under ionViewDidLoad Commented Apr 29, 2017 at 4:57

1 Answer 1

1
let storage = await storage.ready();
let val =     await this.storage.get('ip_address');
this.ip_address = val.toPromise();
Sign up to request clarification or add additional context in comments.

4 Comments

storage.ready() does not return anything in the Promise result and certainly not the storage object, it just gets fulfilled,.The dbPromise that _is indeed returned by the function just keeps the promise chaining alive and is already picked up by await, no need to account for it. And in the first line you apply ready() just to storage, but get() in the second to this.storage. Those are two completely distinct objects. If the purpose was, to pick up the Promise returned by ready() in the "traditional" then-chain, you will never get it this way. Did you try out the code before posting it?
menat to write _dbPromise, but /_\w/ gets converted to italics by the markup bot...
github.com/driftyco/ionic-storage/blob/master/src/… - * @returns {Promise<LocalForage>} Returns a promise that resolves when the store is ready
That is correct, but I don't see the point in assigning the return value of .ready() to something, it just adds confusion. And this very confusion is showing in first applying .ready() to storage, but .get() to this.storage. You are comparing apples to oranges. Or, if, as I am assuming, your three lines of code are supposed to go inside of getFromStorageAsync(), it simply would throw an error, because storage is not defined inside of getFromStorageAsync(), only after you have superfluously assigned it the return value from ready(). This is why I asked if you had checked your code.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.