首页 > 解决方案 > 如何通过get请求将mongodb的数据作为数组存储在本地存储中

问题描述

我有一个服务,它发送一个 get 请求来从我的 mongodb 集合中获取数据,我也想将传入的数据作为一个数组存储到 localstorage 中。

API.SERVICE.TS

getStorageLocationsList(){
    this.setHeader();
    return this.http.get(this.localURL + 'storagelocation/view' + '?userplant=' + this.userplant , {headers:this.appHeader});
  }

组件.TS

this._api.getStorageLocationsList().subscribe(res => {
      this.storagelocationsList = res as Event[];
      //console.log(res);
    }, err => {
      console.log(err);
    });
//I want some more code here to store the data in res to the localstorage

标签: angularmongodbapigetlocal-storage

解决方案


this._api.getStorageLocationsList().subscribe(res => {
      this.storagelocationsList = res as Event[];
      //console.log(res);
      let storagelocationsList = ['A', 'B']; // suppose this is your api response
      localStorage.setItem('locationList', JSON.stringify(storagelocationsList));  
    }, err => {
      console.log(err);
    });

对于获取:JSON.parse(localStorage.getItem('locationList'));

有关 localStorage 的更多操作,请检查


推荐阅读