Storage.set 似乎不起作用

Storage.set does not seem to be working

我正在尝试使用 Storage import {Storage} from '@ionic/storage' 在 ionic 2 中存储来自 firebase 列表的对象列表。但是当我设置存储时,存储的数组是空的。

this.api.getDataListAsPromise(`/programLists`).then(programLists => {
    let programListArr = [];
    new Promise(res => {
      res( forEach(programLists, value => { programListArr[value.$key] = value; }) );
    }).then(() => {
      console.log(programListArr);
      this.storage.set('programLists', programListArr);
    });
  });

console.log(programArr)returns

这正是我想要存储在本地存储中的内容。

然而当我this.storage.set('programLists',programListArr);。它将其设置为一个空数组

有人可以解释为什么会这样,是否可以在本地存储中设置创建的对象文字?

您不能在 storage.set() 中存储对象,但您可以这样做:

this.storage.set('key', JSON.stringify(value));

然后获取:

this.storage.get('key').then((value) => {
    let json_value = JSON.parse(value);
});