[英]How to store data of mongodb through get request in local storage as an array
I have a service that sends a get request to fetch data from my mongodb collection and i want to store that incoming data into the localstorage as an array also.我有一个服务,它发送一个 get 请求来从我的 mongodb 集合中获取数据,我也想将传入的数据作为数组存储到本地存储中。
API.SERVICE.TS API.SERVICE.TS
getStorageLocationsList(){
this.setHeader();
return this.http.get(this.localURL + 'storagelocation/view' + '?userplant=' + this.userplant , {headers:this.appHeader});
}
COMPONENT.TS组件.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
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);
});
For Fetch:
JSON.parse(localStorage.getItem('locationList'));
对于获取:
JSON.parse(localStorage.getItem('locationList'));
For more operations of localStorage check this有关 localStorage 的更多操作,请检查此
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.