'I'm receiving a function overload error when making component for API service
I am trying to design an API with a backend server that receives calls from client side. I'm very new at using Typescript (I come from a Java background.) I get an error stating "No overload matches this call."
I created the basis for the services that will be called in this file:
//create functions that can reach backend by making http requests using httpclient
import { Injectable } from '@angular/core';
import {HttpClient} from '@angular/common/http';
import { Observable } from 'rxjs';
import { Employee } from './employee';
import { environment } from 'src/environments/environment';
//makes angular aware that this is your service
@Injectable({providedIn: 'root'})
export class EmployeeService {
//access to backend url connected to environments.ts
private apiServerUrl = environment.apiBaseUrl;
//this allows requests to backend
constructor(private http: HttpClient) {}
//gets list of all employees
//tells http client where to make the requests and type of requests
public getEmployee(): Observable<Employee> {
//common js technique to put variables and strings at same time
return this.http.get<any>(`${this.apiServerUrl}/employee/all` ) //<-- get requrest
}
public addEmployee(employee: Employee): Observable<Employee> {
//common js technique to put variables and strings at same time
return this.http.post<Employee>(`${this.apiServerUrl}/employee/add`, employee); //<-- post request needed for adding
}
public updateEmployee(employee: Employee): Observable<Employee> {
//common js technique to put variables and strings at same time
return this.http.put<Employee>(`${this.apiServerUrl}/employee/add`, employee); //<-- post request needed for adding
}
//deleting empl will not send any response back, and thus use observable<void>
public deleteEmployee(employeeId: number): Observable<void> {
//common js technique to put variables and strings at same time
return this.http.delete<void>(`${this.apiServerUrl}/employee/delete/${employeeId}`); //<-- post request needed for adding
}
}
this is the app.component.ts file
import { HttpErrorResponse } from '@angular/common/http';
import { Component, OnInit } from '@angular/core';
import { Employee } from './employee';
import { EmployeeService } from './employee.service';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent implements OnInit {
public employees: Employee[];
constructor(private employeeService: EmployeeService){}
ngOnInit(){
this.getEmployee();
}
public getEmployee(): void{
//subscribed used so we are notified when data comes back from server
this.employeeService.getEmployee().subscribe(
(response: Employee[]) => {
this.employees = response;
},
(error: HttpErrorResponse) => {
alert(error.message);
}
);
}
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|