Issue
I need to Retrieving out for example all the volunteers from the DB I wrote the service that gets all the volunteers
this is the service:
GetAllVolunteers():Observable<Volunteer>{
return this.http.get<Volunteer>(environment.url+"ward/GetAllVolunteers")
}
here I'm trying to retrieve out the data:
import { Component, OnInit } from '@angular/core';
import { Volunteer } from 'src/app/shared/models/volunteers.models';
import { VolunteerService } from 'src/app/shared/services/volunteer.service';
@Component({
selector: 'app-all-volunteers',
templateUrl: './all-volunteers.component.html',
styleUrls: ['./all-volunteers.component.css']
})
export class AllVolunteersComponent implements OnInit {
allVolunteers: Array<Volunteer> = [];
constructor(public volunteerService: VolunteerService) { }
ngOnInit(): void {
this.volunteerService.GetAllVolunteers().subscribe(
res => {
this.allVolunteers=res
}
)
}
}
this is the function in c# that retrieve all the volunteer:
[Route("GetAllVolunteers")]
public List<volunteerDTO> GetAllVolunteers()
{
return volunteerBL.getAllVolunteers().Select(v => BL.converts.volunteersConvert.convertVolunteerToDTO(v)).ToList(); ;
}
How do I place the result in the array allVolunteers[]?
Thanks in advance
Solution
Your C# api controller action is returning a List of objects :
[Route("GetAllVolunteers")]
public List<volunteerDTO> GetAllVolunteers()
{
return volunteerBL.getAllVolunteers().Select(v => BL.converts.volunteersConvert.convertVolunteerToDTO(v)).ToList(); ;
}
But your service function is expecting a single object :
GetAllVolunteers():Observable<Volunteer>{
return this.http.get<Volunteer>(environment.url+"ward/GetAllVolunteers")
}
It should expect an array instead :
GetAllVolunteers():Observable<Volunteer[]>{
return this.http.get<Volunteer[]>(environment.url+"ward/GetAllVolunteers")
}
Answered By - New Rhino
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.