Julia Ropoulos
Julia Ropoulos

Reputation: 23

Angular undefined attribut but it's not possible

I'm trying to get local JSON data with angular. My component:

import { Component, OnInit } from '@angular/core';
import datalist from '../todolist.json';

console.log(JSON.stringify(datalist));
@Component({
  selector: 'app-todolist',
  templateUrl: './todolist.component.html',
  styleUrls: ['./todolist.component.css']
})
export class TodolistComponent implements OnInit {

  // will contain all data from ../todolist.json
  lists: any[];
  todolist: any;

  constructor() {}

  ngOnInit() {
    const test = Object.keys(datalist);
    for (const prop of test) {
      this.lists.push(datalist[prop]);
    }
  }
}

And I get this error:

ERROR TypeError: "this.lists is undefined"

What am I doing wrong ? I really don't understand, of course, that this.lists is defined...

Upvotes: 0

Views: 63

Answers (1)

Sajeetharan
Sajeetharan

Reputation: 222582

You need to initialize it as follows

 lists: any[] = [];

Upvotes: 1

Related Questions