-3

storage. get function return null value out side the function. here is my code

username:any;

this.storage.get('user').then((value) => {
       this.username = value;
    });

console.log(this.username);

output : undefined;
Fateme Fazli
  • 11,582
  • 2
  • 31
  • 48
Videsh Chauhan
  • 371
  • 2
  • 18

2 Answers2

1

Why are you using function ? You can simply get the store value with the following code:

this.username = JSON.parse(localStorage.getItem('user'));
  • i want to store array but it provide string – Videsh Chauhan Apr 04 '18 at 06:51
  • JSON.parse will convert string to array. –  Apr 04 '18 at 06:55
  • this is my array { "user_type": "shipper", "name": "testuser", "mobile_number": "1234567890", "updated_at": "2018-04-04 06:57:42", "created_at": "2018-04-04 06:57:42", "id": 40 } localStorage.setItem('user', auth); first of all i am set the item but when i am getting the result this show me the error "JSON.parse: unexpected character at line 1 column 2 of the JSON data" here is my code for getItem this.username = JSON.parse(localStorage.getItem('user')); – Videsh Chauhan Apr 04 '18 at 07:00
  • `localStorage.setItem('user', JSON.stringify(auth));` save the auth data as string not object . –  Apr 04 '18 at 07:05
  • its working ....thank you so much :) – Videsh Chauhan Apr 04 '18 at 07:11
  • most welcome :) –  Apr 04 '18 at 07:17
0

if you want to use local storage:

username:any;

this.username = localStorage.getItem('user');

console.log(this.username);
Fateme Fazli
  • 11,582
  • 2
  • 31
  • 48