【问题标题】:How to store data of mongodb through get request in local storage as an array如何通过get请求将mongodb的数据作为数组存储在本地存储中
【发布时间】:2020-03-14 15:37:17
【问题描述】:

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

API.SERVICE.TS

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

COMPONENT.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

【问题讨论】:

    标签: angular mongodb api get local-storage


    【解决方案1】:
    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操作请查看this

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-01-17
      • 2019-08-16
      • 2015-12-23
      • 1970-01-01
      相关资源
      最近更新 更多