尝试从本地存储检索数据时出现问题
Problems trying to retrieve data from local storage
我正在尝试从这个用 IONIC 和 ANGULAR 制作的应用程序中的离子本地存储中检索一些数据。
仍然看不到我在做什么,但是一旦进程被触发,数据就不会暴露。
假设我以这种方式安装了所有必要的插件后,将数据设置在我的离子存储中
:
DataStorageService
import { Storage } from "@ionic/storage";
allMoviesfavorites: MovieSelectedDetails[] = [];
saveAtStorage(movieToSave: MovieSelectedDetails) {
....asigning some value to variable allMoviesfavorites...
this.storage.set("favorites", this.allMoviesfavorites);
}
同样在同一个服务中,我建立了以这种方式检索它的方法
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;
});
}
如您所见,只需到达我在那里设置的所有数据的本地存储容器,一旦到达那里,无论我得到什么结果,都将分配给先前初始化为空数组的变量 allMoviesFavorite。
然后在我想公开该数据的元素上,我在 ngOnInit 方法上触发了一个调用服务并执行任务的函数,将从服务带来的数据分配给变量 moviesInFavorite,这将在 HTML 以图形方式显示所有数据。
我还记录了带来的任何数据以进行检查,但我没有收到任何数据
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);
}
我该如何改善这个问题?
问题是您在从存储加载数据的异步代码完成之前返回 allMoviesfavorites
。
这应该有效:
loadfavoritesinStorage() {
return this.storage.get("favorites").then((result) => {
if (result == null) {
result = [];
}
this.allMoviesfavorites = result;
return this.allMoviesfavorites; // <-- add the return here!
});
}
我正在尝试从这个用 IONIC 和 ANGULAR 制作的应用程序中的离子本地存储中检索一些数据。 仍然看不到我在做什么,但是一旦进程被触发,数据就不会暴露。
假设我以这种方式安装了所有必要的插件后,将数据设置在我的离子存储中 :
DataStorageService
import { Storage } from "@ionic/storage";
allMoviesfavorites: MovieSelectedDetails[] = [];
saveAtStorage(movieToSave: MovieSelectedDetails) {
....asigning some value to variable allMoviesfavorites...
this.storage.set("favorites", this.allMoviesfavorites);
}
同样在同一个服务中,我建立了以这种方式检索它的方法
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;
});
}
如您所见,只需到达我在那里设置的所有数据的本地存储容器,一旦到达那里,无论我得到什么结果,都将分配给先前初始化为空数组的变量 allMoviesFavorite。
然后在我想公开该数据的元素上,我在 ngOnInit 方法上触发了一个调用服务并执行任务的函数,将从服务带来的数据分配给变量 moviesInFavorite,这将在 HTML 以图形方式显示所有数据。 我还记录了带来的任何数据以进行检查,但我没有收到任何数据
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);
}
我该如何改善这个问题?
问题是您在从存储加载数据的异步代码完成之前返回 allMoviesfavorites
。
这应该有效:
loadfavoritesinStorage() {
return this.storage.get("favorites").then((result) => {
if (result == null) {
result = [];
}
this.allMoviesfavorites = result;
return this.allMoviesfavorites; // <-- add the return here!
});
}