0

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

5
  • 2
    What part doesn't work? It seems fine except a missing semicolon after this.allVolunteers=res Commented Mar 16, 2022 at 15:03
  • I put a semicolon and it's still wrong Commented Mar 16, 2022 at 15:05
  • this is the error: Type 'Volunteer' is missing the following properties from type 'Volunteer[]': length, pop,push, concat, and 28 more.ts(2740) Commented Mar 16, 2022 at 15:06
  • You should use the get function using Volunteer[] instead of Volunteer as a generic type. Commented Mar 16, 2022 at 15:11
  • What a get function? Commented Mar 16, 2022 at 15:17

1 Answer 1

2

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")
}
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.