TypeError: Cannot read property 'length' of undefined in Angular app

Snk

I'm creating a quiz app for practice and needs to display one MCQ at a time. Coded the following but facing an error as said in title.

By googling other similar questions on Stack Overflow,I did try their solutions: Tried console logging, if there was any file missing from Node_Modules/typescript but everything was perfect. Also tried to update Node_Modules and CLI to latest version, but no luck.

Quiz.component.ts:

export class QuizComponent implements OnInit {
  radio1: string;
  value: string;
  target: any;
  quizName = 'QUIZ APPLICATION';
  counter=0;
questArray: any[];
questArray1:any[];
questArray2: any[];

  constructor(private newService: MyDataService) //service define
   { }
  ngOnInit() { 
    this.newService.fetchData()
    .subscribe(response => this.questArray = response.json());
  `   `
  this.callingArray();  

  }

    callingArray() {

    for(var i=0;i<=this.questArray.length;i++)
    {
        this.questArray1=this.questArray[i];
        this.questArray2=this.questArray[i+1];   
    }
     }

Service.ts:

export class MyDataService {
  constructor(private http: Http) 
  {}
  fetchData()
      {
        return this.http
        .get('../assets/questions.json');
      }   
}

I'm really not getting what is being missed!

JSON:

[  
  {
    "QUES":"The Government of India (GoI) has launched the web-based application e-FRRO scheme for foreigners. What does ‘FRRO’ stands for ?",
    "OP":[ "Foreigners Regional Registration Office", "Foreigners Registry Registration Office", "Foreigners Reacceptance Registration Office", "Foreigners Regaining Registration Office" ]
},

  {

        "QUES": "H",
        "OP":["ADASD" , "ASDAD", "ASDASD", "ASDADS"]

    }

]

HTML:

<div *ngFor="let abc of questArray1;let j = index" >

           {{j+1}}.{{abc.QUES}}

        <br>
      <input type="radio" name="s">{{abc.OP[j]}}

      <br>
      <input type="radio" name="s">{{abc.OP[j]}}

      <br>
      <input type="radio" name="s">{{abc.OP[j]}}

      <br>
      <input type="radio" name="s">{{abc.OP[j]}}

      <br>

        </div>
dee zg

your array iteration happens before you get any data from observable. try modifying your code to something like this:

this.newService.fetchData().subscribe(response => {
   this.questArray = response.json();
   this.callingArray();
});

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Uncaught TypeError: Cannot read property 'length' of undefined

From Dev

Uncaught TypeError: Cannot read property 'length' of undefined(…)

From Dev

opencv - TypeError: Cannot read property 'length' of undefined

From Dev

Angular nested objects - TypeError: Cannot read property 'app' of undefined

From Dev

TypeError: Cannot read property '0' of undefined in angular app

From Dev

(Angular http$): Cannot read property 'length' of undefined

From Dev

(Angular http$): Cannot read property 'length' of undefined

From Dev

Uncaught TypeError: Cannot read property 'app' of undefined

From Dev

Angular TypeError cannot read property 'then' of undefined

From Dev

TypeError: Cannot read property 'childNodes' of undefined angular

From Dev

Angular TypeError: Cannot read property 'then' of undefined

From Dev

Angular TypeError cannot read property 'then' of undefined

From Dev

Angular - TypeError: cannot read property 'push' of undefined

From Dev

Angular - TypeError: Cannot read property 'subscribe' of undefined

From Dev

TypeError: Cannot read property 'length' of undefined in QML array

From Dev

Uncaught TypeError: Cannot read property 'length' of undefined when using setInterval

From Dev

Uncaught TypeError: Cannot read property 'length' of undefined JQUERY autocomplete

From Dev

DataTables - Uncaught TypeError: Cannot read property 'length' of undefined

From Dev

d3 TypeError: Cannot read property 'length' of undefined

From Dev

Getting Uncaught TypeError: Cannot read property 'length' of undefined on leafletjs

From Dev

.ajax JSON TypeError: Cannot read property 'length' of undefined

From Dev

clipboardData - Uncaught TypeError: Cannot read property 'length' of undefined

From Dev

Uncaught TypeError: Cannot read property 'length' of undefined when there is a parent

From Dev

Javascript: Simple Function - Uncaught TypeError: Cannot read property 'length' of undefined

From Dev

"Uncaught TypeError: Cannot read property 'length' of undefined" after defining var

From Dev

jquery error uncaught typeerror: cannot read property 'length' of undefined

From Dev

Ajax Uncaught TypeError: Cannot read property 'length' of undefined

From Dev

TypeError: Cannot read property 'length' of undefined array shuffle in react

From Dev

cannot read property'length' of undefined

Related Related

  1. 1

    Uncaught TypeError: Cannot read property 'length' of undefined

  2. 2

    Uncaught TypeError: Cannot read property 'length' of undefined(…)

  3. 3

    opencv - TypeError: Cannot read property 'length' of undefined

  4. 4

    Angular nested objects - TypeError: Cannot read property 'app' of undefined

  5. 5

    TypeError: Cannot read property '0' of undefined in angular app

  6. 6

    (Angular http$): Cannot read property 'length' of undefined

  7. 7

    (Angular http$): Cannot read property 'length' of undefined

  8. 8

    Uncaught TypeError: Cannot read property 'app' of undefined

  9. 9

    Angular TypeError cannot read property 'then' of undefined

  10. 10

    TypeError: Cannot read property 'childNodes' of undefined angular

  11. 11

    Angular TypeError: Cannot read property 'then' of undefined

  12. 12

    Angular TypeError cannot read property 'then' of undefined

  13. 13

    Angular - TypeError: cannot read property 'push' of undefined

  14. 14

    Angular - TypeError: Cannot read property 'subscribe' of undefined

  15. 15

    TypeError: Cannot read property 'length' of undefined in QML array

  16. 16

    Uncaught TypeError: Cannot read property 'length' of undefined when using setInterval

  17. 17

    Uncaught TypeError: Cannot read property 'length' of undefined JQUERY autocomplete

  18. 18

    DataTables - Uncaught TypeError: Cannot read property 'length' of undefined

  19. 19

    d3 TypeError: Cannot read property 'length' of undefined

  20. 20

    Getting Uncaught TypeError: Cannot read property 'length' of undefined on leafletjs

  21. 21

    .ajax JSON TypeError: Cannot read property 'length' of undefined

  22. 22

    clipboardData - Uncaught TypeError: Cannot read property 'length' of undefined

  23. 23

    Uncaught TypeError: Cannot read property 'length' of undefined when there is a parent

  24. 24

    Javascript: Simple Function - Uncaught TypeError: Cannot read property 'length' of undefined

  25. 25

    "Uncaught TypeError: Cannot read property 'length' of undefined" after defining var

  26. 26

    jquery error uncaught typeerror: cannot read property 'length' of undefined

  27. 27

    Ajax Uncaught TypeError: Cannot read property 'length' of undefined

  28. 28

    TypeError: Cannot read property 'length' of undefined array shuffle in react

  29. 29

    cannot read property'length' of undefined

HotTag

Archive