0
Error: Cannot find a differ supporting Object '[Object Object]' of type 'Object'. Ngfor only Supports Binding to Iterables such as Arrays.
I have model:
import { Classroom } from "./classroom.model";
export class Student {
public id : string;
public name: string;
public classroom: Classroom[];
public mother: string;
public phone_mother: string;
public father: string;
public phone_father: string;
public address: string;
constructor(id : string, name: string, classroom: Classroom[], mother: string, phone_mother: string, father: string, phone_father: string, address: string, ){
this.id = id,
this.name = name;
this.mother = mother;
this.father = father;
this.phone_mother = phone_mother;
this.phone_father = phone_father;
this.address = address;
this.classroom = classroom;
}
}
My Service:
import { Injectable } from '@angular/core';
import { Http, Response } from '@angular/http';
import { Observable, Subject } from 'rxjs';
import { map } from 'rxjs/operators';
import { Student } from "../models/student.model";
import { Classroom } from "../models/classroom.model";
@Injectable()
export class StudentsServices
{
private _url = 'https://localhost:3000/api/aluno'
constructor(private http: Http){}
getStudents(){
return this.http
.get(this._url)
.pipe(map((response : Response) => {
return <Student[]>response.json();
}));
}
getEstudantes(): Observable<Student[]> {
return this.http.get(this._url)
.pipe(map(res=>res.json()));
}
}
My Component:
import { Component, OnInit } from '@angular/core';
import { StudentsServices } from '../services/students.services';
import { Student } from '../models/student.model';
@Component({
selector: 'app-students',
templateUrl: './students.component.html',
styleUrls: ['./students.component.css'],
providers: [StudentsServices]
})
export class StudentsComponent implements OnInit {
_students: Student[] = [];
constructor(private services: StudentsServices) { }
getStudents(): void {
this.services.getStudents()
.subscribe(
data => this._students = data,
error => console.log("Student Service Error: " + error)
)
}
getEstudantes() {
this.services.getUsers()
.subscribe(
data => this._students = data,
error => console.log("Student Service Error: " + error));
}
ngOnInit() {
this.getStudents();
//this.getEstudantes();
console.log(this._students);
}
}
and this is my HTML
<div class="row">
<table class="table">
<thead class="thead-inverse">
<tr>
<th class="text-center">ID</th>
<th class="text-center">Nome</th>
<th class="text-center">Mãe</th>
<th class="text-center">Pai</th>
<th class="text-center">Telefone da mãe</th>
<th class="text-center">Telefone do Pai</th>
<th class="text-center">Endereço</th>
<th class="text-center">Turma</th>
</tr>
</thead>
<tbody>
<tr *ngFor="let student of _students">
<td class="text-center">{{student.id}}</td>
<td class="text-center">{{student.name}}</td>
<td class="text-center">{{student.mother}}</td>
<td class="text-center">{{student.father}}</td>
<td class="text-center">{{student.phone_father}}</td>
<td class="text-center">{{student.phone_mother}}</td>
<td class="text-center">{{student.address}}</td>
</tr>
</tbody>
</table>
</div>
tries Let student of _student.students
– Marconi
I’ve tried, it says students don’t exist ):
– vdschuck
Make a Demon in stackblitz
– Marconi
Please do not post image as code. It is possible to edit your question with the code itself?
– Eduardo Vargas
https://pt.meta.stackoverflow.com/questions/5483/manual-de-como-n%C3%83o-fazer-perguntas/5485#5485
– Eduardo Vargas
If you post your code as text I do a stackblitz and see what’s wrong.
– Marconi
I’ve changed it to code, if you can help me.
– vdschuck
What comes out when you play the url in your browser? You can add a print?
– Marconi
{"students":[{"_id":"5b0af0b35af000143a39af","name":"Adriano","class":{"_id":"5b0aee26539af000143a39ab","Description":"CLASS 304"}},{"_id":"5b0af0b7539af000143a39b0","name":"Such","class":{"_id":"5b0aee26539af000143a39ab","Description":"CLASS 304"}},{"_id":"5b0af539af000143a39b1","name"":"Gustavo","class":{"_id":"5b0aee26539af000143a39ab","Description":"TURMA 304"}},{"_id":"5b0af0be539af000143a39b2","name":"Vinicius","class":{"_id":"5b0aee26539af000143a39ab","Description":"TURMA 304"}}],"total":4}
– vdschuck