I can't show the content of the objects of my service.
up vote
0
down vote
favorite
I want to put on a HTML component, the variables of objects which become from a service. And I can't.
My component is:
import { Component, OnInit } from '@angular/core';
import { FormGroup, FormControl } from '@angular/forms';
import { Profesional, ProfesionalService} from '../../profesional.service';
@Component({
selector: 'app-gestion-profesionales',
templateUrl: './gestion-profesionales.component.html',
styleUrls: ['./gestion-profesionales.component.css']
})
export class GestionProfesionalesComponent implements OnInit {
prof = new Array<Profesional>();
tags;
constructor(private profesionalService: ProfesionalService) { }
ngOnInit() {
this.allProf();
}
allProf(): void {
this.profesionalService.getProfesionales()
.subscribe(data => {
this.prof= data;
console.log(this.prof);
});
}
}
My service is :
import { Injectable } from '@angular/core';
import { HttpClient, HttpHeaders } from '@angular/common/http';
import { Observable } from 'rxjs/Observable';
import { of } from 'rxjs/observable/of';
import { catchError, map, tap } from 'rxjs/operators';
export interface Profesional {
ID: number;
Name: string;
College: string;
DNI: string;
Surname: string;
Email: string;
Password: string;
Phone: string;
Photo: string;
}
const httpOptions = {
headers: new HttpHeaders({ 'Content-Type': 'application/json' })
};
@Injectable()
export class ProfesionalService {
private profesionalesUrl = 'https://h205.eps.ua.es:8080/profesionales'; // URL to web api
constructor(
private http: HttpClient
) { }
/** GET obtenemos todos los profesionales */
getProfesionales (): Observable<Profesional> {
return this.http.get<Profesional>(this.profesionalesUrl)
.pipe(
tap(profesionales => this.log(`fetched profesionales`)),
catchError(this.handleError('getProfesionales', ))
);
}
}
And when I do the request, everything is OK. The JSON response is like this:
Object results:
Array(35)
0: {ID: "1", DNI: "71955507F", College: "mimi", Name: "pepe", Surname: "popo", …}
1: {ID: "_09y4nb7b1", DNI: "434632tnm", College: "siuno", Name: "Matasanos", Surname: "Berenguer Pastor", …}
So, I have problems to show on my HTML component the info. I want to do it with ng-for, but it doesn't work. It appears this error: Cannot find a differ supporting object '[object Object]' of type 'object'. NgFor only supports binding to Iterables such as Arrays.
<table>
<tr>
<th>Name</th>
<th>Surname</th>
<th>Phone number</th>
<th>Email</th>
</tr>
<tbody>
<tr *ngFor="let item of prof">
<td>{{ item.Name }}</td>
<td>{{ item.Surname }}</td>
<td>{{ item.Phone }}</td>
<td>{{ item.Email }}</td>
</tr>
</tbody>
</table>
Maybe is due to the variable prof that is form by Profesional instances. I don't know how to show the info in the correct way.
angular service ngfor
add a comment |
up vote
0
down vote
favorite
I want to put on a HTML component, the variables of objects which become from a service. And I can't.
My component is:
import { Component, OnInit } from '@angular/core';
import { FormGroup, FormControl } from '@angular/forms';
import { Profesional, ProfesionalService} from '../../profesional.service';
@Component({
selector: 'app-gestion-profesionales',
templateUrl: './gestion-profesionales.component.html',
styleUrls: ['./gestion-profesionales.component.css']
})
export class GestionProfesionalesComponent implements OnInit {
prof = new Array<Profesional>();
tags;
constructor(private profesionalService: ProfesionalService) { }
ngOnInit() {
this.allProf();
}
allProf(): void {
this.profesionalService.getProfesionales()
.subscribe(data => {
this.prof= data;
console.log(this.prof);
});
}
}
My service is :
import { Injectable } from '@angular/core';
import { HttpClient, HttpHeaders } from '@angular/common/http';
import { Observable } from 'rxjs/Observable';
import { of } from 'rxjs/observable/of';
import { catchError, map, tap } from 'rxjs/operators';
export interface Profesional {
ID: number;
Name: string;
College: string;
DNI: string;
Surname: string;
Email: string;
Password: string;
Phone: string;
Photo: string;
}
const httpOptions = {
headers: new HttpHeaders({ 'Content-Type': 'application/json' })
};
@Injectable()
export class ProfesionalService {
private profesionalesUrl = 'https://h205.eps.ua.es:8080/profesionales'; // URL to web api
constructor(
private http: HttpClient
) { }
/** GET obtenemos todos los profesionales */
getProfesionales (): Observable<Profesional> {
return this.http.get<Profesional>(this.profesionalesUrl)
.pipe(
tap(profesionales => this.log(`fetched profesionales`)),
catchError(this.handleError('getProfesionales', ))
);
}
}
And when I do the request, everything is OK. The JSON response is like this:
Object results:
Array(35)
0: {ID: "1", DNI: "71955507F", College: "mimi", Name: "pepe", Surname: "popo", …}
1: {ID: "_09y4nb7b1", DNI: "434632tnm", College: "siuno", Name: "Matasanos", Surname: "Berenguer Pastor", …}
So, I have problems to show on my HTML component the info. I want to do it with ng-for, but it doesn't work. It appears this error: Cannot find a differ supporting object '[object Object]' of type 'object'. NgFor only supports binding to Iterables such as Arrays.
<table>
<tr>
<th>Name</th>
<th>Surname</th>
<th>Phone number</th>
<th>Email</th>
</tr>
<tbody>
<tr *ngFor="let item of prof">
<td>{{ item.Name }}</td>
<td>{{ item.Surname }}</td>
<td>{{ item.Phone }}</td>
<td>{{ item.Email }}</td>
</tr>
</tbody>
</table>
Maybe is due to the variable prof that is form by Profesional instances. I don't know how to show the info in the correct way.
angular service ngfor
Welcome on StackOverFlow! What happens if you assign yourprof
variable to an hard written array of values ? I mean, just copy a slice of the result from the api into your code and see.
– Kapcash
Nov 20 at 16:10
can you share the JSON instead of Object Result?
– Code-EZ
Nov 20 at 17:03
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I want to put on a HTML component, the variables of objects which become from a service. And I can't.
My component is:
import { Component, OnInit } from '@angular/core';
import { FormGroup, FormControl } from '@angular/forms';
import { Profesional, ProfesionalService} from '../../profesional.service';
@Component({
selector: 'app-gestion-profesionales',
templateUrl: './gestion-profesionales.component.html',
styleUrls: ['./gestion-profesionales.component.css']
})
export class GestionProfesionalesComponent implements OnInit {
prof = new Array<Profesional>();
tags;
constructor(private profesionalService: ProfesionalService) { }
ngOnInit() {
this.allProf();
}
allProf(): void {
this.profesionalService.getProfesionales()
.subscribe(data => {
this.prof= data;
console.log(this.prof);
});
}
}
My service is :
import { Injectable } from '@angular/core';
import { HttpClient, HttpHeaders } from '@angular/common/http';
import { Observable } from 'rxjs/Observable';
import { of } from 'rxjs/observable/of';
import { catchError, map, tap } from 'rxjs/operators';
export interface Profesional {
ID: number;
Name: string;
College: string;
DNI: string;
Surname: string;
Email: string;
Password: string;
Phone: string;
Photo: string;
}
const httpOptions = {
headers: new HttpHeaders({ 'Content-Type': 'application/json' })
};
@Injectable()
export class ProfesionalService {
private profesionalesUrl = 'https://h205.eps.ua.es:8080/profesionales'; // URL to web api
constructor(
private http: HttpClient
) { }
/** GET obtenemos todos los profesionales */
getProfesionales (): Observable<Profesional> {
return this.http.get<Profesional>(this.profesionalesUrl)
.pipe(
tap(profesionales => this.log(`fetched profesionales`)),
catchError(this.handleError('getProfesionales', ))
);
}
}
And when I do the request, everything is OK. The JSON response is like this:
Object results:
Array(35)
0: {ID: "1", DNI: "71955507F", College: "mimi", Name: "pepe", Surname: "popo", …}
1: {ID: "_09y4nb7b1", DNI: "434632tnm", College: "siuno", Name: "Matasanos", Surname: "Berenguer Pastor", …}
So, I have problems to show on my HTML component the info. I want to do it with ng-for, but it doesn't work. It appears this error: Cannot find a differ supporting object '[object Object]' of type 'object'. NgFor only supports binding to Iterables such as Arrays.
<table>
<tr>
<th>Name</th>
<th>Surname</th>
<th>Phone number</th>
<th>Email</th>
</tr>
<tbody>
<tr *ngFor="let item of prof">
<td>{{ item.Name }}</td>
<td>{{ item.Surname }}</td>
<td>{{ item.Phone }}</td>
<td>{{ item.Email }}</td>
</tr>
</tbody>
</table>
Maybe is due to the variable prof that is form by Profesional instances. I don't know how to show the info in the correct way.
angular service ngfor
I want to put on a HTML component, the variables of objects which become from a service. And I can't.
My component is:
import { Component, OnInit } from '@angular/core';
import { FormGroup, FormControl } from '@angular/forms';
import { Profesional, ProfesionalService} from '../../profesional.service';
@Component({
selector: 'app-gestion-profesionales',
templateUrl: './gestion-profesionales.component.html',
styleUrls: ['./gestion-profesionales.component.css']
})
export class GestionProfesionalesComponent implements OnInit {
prof = new Array<Profesional>();
tags;
constructor(private profesionalService: ProfesionalService) { }
ngOnInit() {
this.allProf();
}
allProf(): void {
this.profesionalService.getProfesionales()
.subscribe(data => {
this.prof= data;
console.log(this.prof);
});
}
}
My service is :
import { Injectable } from '@angular/core';
import { HttpClient, HttpHeaders } from '@angular/common/http';
import { Observable } from 'rxjs/Observable';
import { of } from 'rxjs/observable/of';
import { catchError, map, tap } from 'rxjs/operators';
export interface Profesional {
ID: number;
Name: string;
College: string;
DNI: string;
Surname: string;
Email: string;
Password: string;
Phone: string;
Photo: string;
}
const httpOptions = {
headers: new HttpHeaders({ 'Content-Type': 'application/json' })
};
@Injectable()
export class ProfesionalService {
private profesionalesUrl = 'https://h205.eps.ua.es:8080/profesionales'; // URL to web api
constructor(
private http: HttpClient
) { }
/** GET obtenemos todos los profesionales */
getProfesionales (): Observable<Profesional> {
return this.http.get<Profesional>(this.profesionalesUrl)
.pipe(
tap(profesionales => this.log(`fetched profesionales`)),
catchError(this.handleError('getProfesionales', ))
);
}
}
And when I do the request, everything is OK. The JSON response is like this:
Object results:
Array(35)
0: {ID: "1", DNI: "71955507F", College: "mimi", Name: "pepe", Surname: "popo", …}
1: {ID: "_09y4nb7b1", DNI: "434632tnm", College: "siuno", Name: "Matasanos", Surname: "Berenguer Pastor", …}
So, I have problems to show on my HTML component the info. I want to do it with ng-for, but it doesn't work. It appears this error: Cannot find a differ supporting object '[object Object]' of type 'object'. NgFor only supports binding to Iterables such as Arrays.
<table>
<tr>
<th>Name</th>
<th>Surname</th>
<th>Phone number</th>
<th>Email</th>
</tr>
<tbody>
<tr *ngFor="let item of prof">
<td>{{ item.Name }}</td>
<td>{{ item.Surname }}</td>
<td>{{ item.Phone }}</td>
<td>{{ item.Email }}</td>
</tr>
</tbody>
</table>
Maybe is due to the variable prof that is form by Profesional instances. I don't know how to show the info in the correct way.
angular service ngfor
angular service ngfor
asked Nov 20 at 14:48
qbvp
11
11
Welcome on StackOverFlow! What happens if you assign yourprof
variable to an hard written array of values ? I mean, just copy a slice of the result from the api into your code and see.
– Kapcash
Nov 20 at 16:10
can you share the JSON instead of Object Result?
– Code-EZ
Nov 20 at 17:03
add a comment |
Welcome on StackOverFlow! What happens if you assign yourprof
variable to an hard written array of values ? I mean, just copy a slice of the result from the api into your code and see.
– Kapcash
Nov 20 at 16:10
can you share the JSON instead of Object Result?
– Code-EZ
Nov 20 at 17:03
Welcome on StackOverFlow! What happens if you assign your
prof
variable to an hard written array of values ? I mean, just copy a slice of the result from the api into your code and see.– Kapcash
Nov 20 at 16:10
Welcome on StackOverFlow! What happens if you assign your
prof
variable to an hard written array of values ? I mean, just copy a slice of the result from the api into your code and see.– Kapcash
Nov 20 at 16:10
can you share the JSON instead of Object Result?
– Code-EZ
Nov 20 at 17:03
can you share the JSON instead of Object Result?
– Code-EZ
Nov 20 at 17:03
add a comment |
1 Answer
1
active
oldest
votes
up vote
0
down vote
you got JSON array not Javascript Object from api, that's why when javascript try to loop that's show the error, because your try gets an object you don't have.
Use JSON.parse() function in order to convert it to JS object.
change these line
this.prof = data
with
this.prof = JSON.parse(data);
so it's like this
ngOnInit() {
this.allProf();
}
allProf(): void {
this.profesionalService.getProfesionales()
.subscribe(data => {
this.prof = JSON.parse(data);
console.log(this.prof);
});
}
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53395570%2fi-cant-show-the-content-of-the-objects-of-my-service%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
you got JSON array not Javascript Object from api, that's why when javascript try to loop that's show the error, because your try gets an object you don't have.
Use JSON.parse() function in order to convert it to JS object.
change these line
this.prof = data
with
this.prof = JSON.parse(data);
so it's like this
ngOnInit() {
this.allProf();
}
allProf(): void {
this.profesionalService.getProfesionales()
.subscribe(data => {
this.prof = JSON.parse(data);
console.log(this.prof);
});
}
add a comment |
up vote
0
down vote
you got JSON array not Javascript Object from api, that's why when javascript try to loop that's show the error, because your try gets an object you don't have.
Use JSON.parse() function in order to convert it to JS object.
change these line
this.prof = data
with
this.prof = JSON.parse(data);
so it's like this
ngOnInit() {
this.allProf();
}
allProf(): void {
this.profesionalService.getProfesionales()
.subscribe(data => {
this.prof = JSON.parse(data);
console.log(this.prof);
});
}
add a comment |
up vote
0
down vote
up vote
0
down vote
you got JSON array not Javascript Object from api, that's why when javascript try to loop that's show the error, because your try gets an object you don't have.
Use JSON.parse() function in order to convert it to JS object.
change these line
this.prof = data
with
this.prof = JSON.parse(data);
so it's like this
ngOnInit() {
this.allProf();
}
allProf(): void {
this.profesionalService.getProfesionales()
.subscribe(data => {
this.prof = JSON.parse(data);
console.log(this.prof);
});
}
you got JSON array not Javascript Object from api, that's why when javascript try to loop that's show the error, because your try gets an object you don't have.
Use JSON.parse() function in order to convert it to JS object.
change these line
this.prof = data
with
this.prof = JSON.parse(data);
so it's like this
ngOnInit() {
this.allProf();
}
allProf(): void {
this.profesionalService.getProfesionales()
.subscribe(data => {
this.prof = JSON.parse(data);
console.log(this.prof);
});
}
answered Nov 20 at 16:12
Muhammad Al Faris
263
263
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53395570%2fi-cant-show-the-content-of-the-objects-of-my-service%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Welcome on StackOverFlow! What happens if you assign your
prof
variable to an hard written array of values ? I mean, just copy a slice of the result from the api into your code and see.– Kapcash
Nov 20 at 16:10
can you share the JSON instead of Object Result?
– Code-EZ
Nov 20 at 17:03