Click here to Skip to main content
15,867,756 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
problem
get error when make service getallemployee

Conversion of type 'Promise<any>' to type 'Employee[]' may be a mistake because neither type sufficiently overlaps with the other. If this was intentional, convert the expression to 'unknown' first.ts(2352)
Conversion of type 'Promise<any>' to type 'Employee[]' may be a mistake because neither type sufficiently overlaps with the other. If th


error done on getemployeelist function
how to solve this problem please?

What I have tried:

export class Employee {
    EmployeeId : number;
    BranchCode:number;
    EmployeeName:string;
    EmployeeAge:number;
    JoinDate:Date;
    BirthDate:Date;
    Active : boolean;
}

import { Injectable } from '@angular/core'
import { HttpClient } from '@angular/common/http'
import {Employee} from'./employee.model'
import { Observable } from 'rxjs'
import 'rxjs/add/operator/map'
import 'rxjs/add/operator/toPromise'
import 'rxjs/add/observable/fromPromise'
import { map } from 'rxjs/operators'

@Injectable()
export class ApiService{
    employeeList : Employee[];
    constructor(private http : HttpClient){}

       
   
        
        getEmployeeList(): Observable<Employee[]> {
          return this.http
              .get('https://localhost:44326/api/Employee')
              .map((response: Response) => {
                  return <Employee[]>response.json();
              })
              .catch(this.handleError);
      }

}
Posted
Updated 22-Mar-19 3:27am
Comments
Bryian Tan 16-Feb-19 23:27pm    
try .map(response => response.json())
ahmed_sa 17-Feb-19 2:41am    
and no need to using array
ahmed_sa 17-Feb-19 2:42am    
or nwhat

1 solution

The error is pretty self-descriptive. this.http.get returns Promise which you unsuccesfully try to convert into your entities.
Use
this.http
.get('https://localhost:44326/api/Employee')
.then(res => //your logic) 
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900