简体   繁体   English

尝试从本地存储检索数据的问题

[英]Problems trying to retrieve data from local storage

Im trying to retrieve some data from ionic local storage in this app made in IONIC and ANGULAR.我试图从这个用 IONIC 和 ANGULAR 制作的应用程序中的离子本地存储中检索一些数据。 Still can't see what i'm ommitting but the data doesn't get exposed once the process is triggered.仍然看不到我忽略了什么,但是一旦触发过程,数据就不会暴露。

Lets say i set the data in my ionic storage afeter having installed all the neccesary plugins in this way :假设我以这种方式安装了所有必要的插件后,在我的离子存储中设置了数据:

DataStorageService

import { Storage } from "@ionic/storage";

allMoviesfavorites: MovieSelectedDetails[] = [];

  saveAtStorage(movieToSave: MovieSelectedDetails) {
     ....asigning some value to variable allMoviesfavorites...

     this.storage.set("favorites", this.allMoviesfavorites);
  }

Also in the same service i establish the method to retrieve it in this way同样在同一个服务中,我建立了以这种方式检索它的方法

DataStorageService

import { Storage } from "@ionic/storage";

 allMoviesfavorites: MovieSelectedDetails[] = [];

constructor( private storage: Storage ) { this.loadfavoritesinStorage(); }
 
OPTION1
loadfavoritesinStorage() {
    this.storage.get("favorites").then((result) => {
      if (result == null) {
        result = [];
      }
      this.allMoviesfavorites = result;
      
    });

    return this.allMoviesfavorites;
  }

OPTION 2
 async loadfavoritesinStorage() {
    return this.storage.get("favorites").then((result) => {

       if (result == null) {
        result = [];
      }
      this.allMoviesfavorites =  result;
      console.log(result);
      

      return this.allMoviesfavorites;
    });
  }

As you see simply reach my local storage container of all the data i have been setting there, and once reached there whatever the result i got would be asigned to the variable allMoviesFavorite previously initialized as an empty array.如您所见,只需到达我在那里设置的所有数据的本地存储容器,一旦到达那里,无论我得到什么结果,都会分配给之前初始化为空数组的变量 allMoviesFavorite。

Then on the element i want to expose that data i trigger on the ngOnInit method a fucntion that calls the service and does the taks, asigning the data brought from service to the variable moviesInFavorite, that would be looped in the HTML to display graphically all data .然后在元素上,我想公开我在 ngOnInit 方法上触发的数据,一个调用服务并执行任务的功能,将从服务带来的数据分配给变量 moviesInFavorite,这将在 HTML 中循环以图形方式显示所有数据. Also i log whatever the data is brought in order to check, but i don't receive any data此外,我记录了为了检查而带来的任何数据,但我没有收到任何数据

Tab

import { DataStorageService } from "../services/data-storage.service";


moviesInFavorites: MovieSelectedDetails[] = [];
  constructor(private storageservice: DataStorageService) {}

  ngOnInit() {
    this.getFavorites();
  }

  async getFavorites() {
    let moviesInFavorites = await this.storageservice.loadfavoritesinStorage();
    console.log(moviesInFavorites);
    return (this.moviesInFavorites = moviesInFavorites);
  }

在此处输入图片说明 在此处输入图片说明

How could i improve the problem?我怎样才能改善这个问题?

The issue is that you're returning allMoviesfavorites before the async code that loads the data from the storage is done.问题是您在从存储加载数据的异步代码allMoviesfavorites之前返回allMoviesfavorites

This should work:这应该有效:

loadfavoritesinStorage() {
  return this.storage.get("favorites").then((result) => {
    if (result == null) {
      result = [];
    }

    this.allMoviesfavorites = result;
    
    return this.allMoviesfavorites; // <-- add the return here!      
  });
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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