简体   繁体   中英

How do I return list/value from ionic2/3 storage

I know this probably isn't the 100% right way to do this, so advice for cleanup is also welcome.

Just learning/getting used to Ionic2 and I'm having trouble returning the Storage Item.

Service:

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

@Injectable()
export class LocationItemService {
  locationItems: LocationItem[] = [];

  constructor(
    private storage: Storage
  ) {}

  getListItems(id): Promise<LocationItem[]> {
    this.storage.get("Location_Items_" + id).then((val) => {
      // This does console out the proper value.
      console.log(val);
      // Thus Promise contains the proper value.
      return Promise.resolve(val);
    });
  }
}

However, when I run my application in development, I get this error: "A function whose declared type is neither 'void' nor 'any' must return a value."

Even if I set this.locationItems, I still need to return the promise so that the next function is not run until the Promise is resolved.

I figured it had to do with the scope being inside of the storage.get, instead of directly within the function itself, and have tried a few workarounds to no avail. Thank you for whatever input you may have!

Oi, I was not using the return before this.storage. As it is already defined as a promise. And thus my return statements were essentially stuck in limbo.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM