1

I'm learning Angular 4. I am trying to display the results of a call to my backend API. I'm getting the data fine. I want to display it. Here is the component:

import { Component, OnInit } from '@angular/core'; 
import { QuizService } from '../services/quiz.service';

@Component({ 
    selector: 'board', 
    templateUrl: './board.component.html', 
    styleUrls: ['./board.component.css']
})

export class Board {
    constructor(private service: QuizService) {}

    quizzes: any[]; 

    ngOnInit() {
        this.service.getQuizzes()
            .subscribe(res => {
                this.quizes = res.json(); 
                console.log(this.quizzes)
            }), error => {
                console.error(error)
            }
    }
}

Here is where I am attempting to use ngFor:

<div className="board"> 
    <div className="board-container">
       <li *ngFor="let quiz of quizzes">
           {{ quiz.title }}
       </li>
    </div>
</div>

This raises this error:

"Board.html:3 ERROR Error: Cannot find a differ supporting object '[object Object]' of type 'object'. NgFor only supports binding to Iterables such as Arrays."

I'm following a tutorial and have done similar things before... can't figure out what I'm doing wrong here. The data IS coming through.

3
  • 2
    Can you show console.log(this.quizes) because I don't know structure Commented Mar 1, 2018 at 19:00
  • What does you quizes array look like? Commented Mar 1, 2018 at 19:00
  • The problem was it needed to be: this.quizes = res.json().quizes The call doesn't return the array, it returns an object containing the array. Fixed it. Commented Mar 1, 2018 at 19:02

1 Answer 1

1

The problem was it needed to be:

this.quizzes = res.json().quizzes

The call doesn't return the array, it returns an object containing the array. Adding the extra .quizzes gets you inside the array and makes the error go away.

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

Comments

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.