0

I'm missing something elementary in my attempt to store and retrieve an array in localstorage in my Angular 4 app.

The app retrieves the array from the server into an 'any' typed variable called aToDo as:

{"qoption":"Trap Surveyor (<em>Competency for CM OAP<\/em>)"}

and stored in localstorage by:

localStorage.setItem('aToDo',JSON.stringify(this.aToDo));

It's retrieved from localstorage as:

this.aToDo = JSON.parse(localStorage.getItem('aToDo'));

A console.log of this.aToDo displays:

{"qoption":"Trap Surveyor (<em>Competency for CM OAP<\/em>)"}

But by this stage, things are stuffed as logging this.aToDo.qoption yields 'undefined' and the *ngFor used to list the array's contents generates the damning error:

Error: Cannot find a differ supporting object '{"qoption":"Trap Surveyor (<em>Competency for CM OAP<\/em>)"}' of type 'string'. 

Where is my error? Thanks/Tom

1 Answer 1

2

Two things:

  1. console.log will output "undefined" by default alongside your output because it's not a return function. That's normal.
  2. aToDo is an object, not an array. You cannot use ngFor on an object.
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for getting me on track. Finally got to the point I wanted by interating objectKeys(aToDo). Cheers/T

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.