Sunday, May 19, 2024
 Popular · Latest · Hot · Upcoming
34
rated 0 times [  41] [ 7]  / answers: 1 / hits: 135330  / 7 Years ago, fri, february 3, 2017, 12:00:00

I have an array like this:



[{name:test, time:Date 2017-02-03T08:38:04.449Z}]


I stored it in localstorage and when I retrieving data from localstorage I got the value:



[object, object]


How can I solve this issue?



config.ts



import { Injectable } from @angular/core;

@Injectable()
export class TokenManager {

public tokenKey: string = 'app_token';

constructor() { }

store(content) {
var contentData;

console.log(inside localstorsge store:, content);
contentData = content.map(
(data) => data.name
)
console.log(contentData:, contentData)
localStorage.setItem(this.tokenKey, content);
}

retrieve() {
console.log(inside localstorage);
let storedToken: any = localStorage.getItem(this.tokenKey);
console.log(storedToken:, storedToken);//====> here this console is[object object]
if (!storedToken) throw 'no token found';
return storedToken;
}

}

More From » angular

 Answers
9

local storage limited to handle only string key/value pairs you can do like below using JSON.stringify and while getting value JSON.parse



var testObject ={name:test, time:Date 2017-02-03T08:38:04.449Z};


Put the object into storage:



localStorage.setItem('testObject', JSON.stringify(testObject));


Retrieve the object from storage:



var retrievedObject = localStorage.getItem('testObject');

console.log('retrievedObject: ', JSON.parse(retrievedObject));

[#59086] Wednesday, February 1, 2017, 7 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
eanskylerg

Total Points: 524
Total Questions: 107
Total Answers: 100

Location: Colombia
Member since Mon, May 2, 2022
2 Years ago
;