1

I am trying to display the results from an array into the html template. I need to display the tasks objects. Currently I can successfully console log the result but when I try to display it I get a blank ion-list. I am retrieving the array from Ionic storage.

[
  {
    "projects": {
      "projectname": "test",
      "dateadded": "09 April 2018, 6:29AM",
      "location": "us",
      "tasks": {
        "0": {
          "taskname": "test",
          "taskdescription": "test",
          "taskdealer": "test",
          "tasksupplier": "Technical",
          "taskdeadline": "2018-04-08"
        },
        "1": {
          "taskname": "test",
          "taskdescription": "test",
          "taskdealer": "test",
          "tasksupplier": "Technical",
          "taskdeadline": "2018-04-08"
        }
      }
    }
  }
]

My attempt

this.storage.get('projectsStore').then(data => {
  for (var i = 0; i < data.length; i++) {
      console.log(data[i].projects.tasks);
      this.tasks = [data[i].projects.tasks];
    }
})

<ion-list>
  <button ion-item no-padding *ngFor="let item of tasks">
    <h2>{{item.taskname}}</h2>
    <p>{{item.taskdeadline}}</p>
  </button>
</ion-list>
2
  • can you show the log of this.tasks Commented Apr 9, 2018 at 4:54
  • { "0":{ "taskname":"test", "taskdescription":"test", "taskdealer":"test", "tasksupplier":"Technical", "taskdeadline":"2018-04-08" }, "1":{ "taskname":"test", "taskdescription":"test", "taskdealer":"test", "tasksupplier":"Technical", "taskdeadline":"2018-04-08" } } Commented Apr 9, 2018 at 4:58

1 Answer 1

2

You cannot repeat Object. Convert it to array like following. It should work like that:

this.storage.get('projectsStore').then(data => {
  for (var i = 0; i < data.length; i++) {
      console.log(data[i].projects.tasks);
      this.tasks = Object.values(data[i].projects.tasks);
    }
})

Check out this fiddle: https://jsfiddle.net/feLnekjb/1/ . Click to see your list getting repeated.

Sign up to request clarification or add additional context in comments.

4 Comments

thanks but I get "[ts] Property 'values' does not exist on type 'ObjectConstructor'."
fixed the above error, but still get an empty display. If I display this <h2>{{item}}</h2> then I get [object, object],[object, object]. If I display this <h2>{{item.taskname}}</h2>, I get nothing
can you log your this.tasks after the above step and show here
[ { "0":{ "taskname":"test", "taskdescription":"test", "taskdealer":"test", "tasksupplier":"Technical", "taskdeadline":"2018-04-08" }, "1":{ "taskname":"test", "taskdescription":"test", "taskdealer":"test", "tasksupplier":"Technical", "taskdeadline":"2018-04-08" } } ]

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.