argument of type is not assignable to parameter of type any[]

i have some issues with my code and this is the error

message: 'The argument of type 'Employee' is not assignable to the parameter of type' any [] '. The 'includes' property is missing in the 'Employee' type.

This is the service

import { Injectable } from '@angular/core';
import { AngularFireDatabase, AngularFireList, AngularFireObject } from 'angularfire2/database';
import { Observable } from 'rxjs';
import { Employee } from '../Employee';
import { FirebaseApp } from 'angularfire2';

@Injectable()
export class EmployeeService {
 
  employees: AngularFireList<any[]>;
  Employee: AngularFireObject<any>;

  constructor( 
    public af: AngularFireDatabase
  ) { 
    this.employees = this.af.list('/employees/employees') as AngularFireList <Employee[]>;
    
  }
getEmployees(): AngularFireList <any> {
  return this.employees ;

}
addEmployee(emp:Employee)
{
  
  return this.employees.push(emp);
}
}

and this is my component

import { Component, OnInit } from '@angular/core';
import { Employee } from '../../Employee';
import { FlashMessagesService } from 'angular2-flash-messages';
import { timeout } from 'q';
import { Router } from '@angular/router';
import { EmployeeService } from '../../services/employee.service';
@Component({
  selector: 'app-add-employee',
  templateUrl: './add-employee.component.html',
  styleUrls: ['./add-employee.component.css']
})
export class AddEmployeeComponent implements OnInit {
  employee:Employee={
    
    firstName:"",
    lastName:"",
    email:"",
    country:"",
    city:"",
    phone:0,
    salary:0
  }
  disableSalary:boolean=true;
  constructor(public fashMessagesService : FlashMessagesService, public router:Router,
  public employeeService : EmployeeService
  ) { }

  ngOnInit() {
    
  }


  mySubmit({value,valid}:{value:Employee,valid:boolean}){
    if(this.disableSalary){
      value.salary=0;
    }

    if (!valid) {
      this.fashMessagesService.show('Please Write Correct Info',{cssClass:'alert-danger',timeout:3000});
      //console.log("not correct data");
      this.router.navigate(['add-employee']);
    }else {
      this.employeeService.addEmployee(value);
      this.fashMessagesService.show('Added Successfully!',{cssClass:'alert-success',timeout:3000});
      this.router.navigate(['/']);
      //console.log(this.employee);

    }
  }

}

i don't know what is the problem exactly, i hope someone get it.

Upvotes: 0

Views: 3255

Answers (1)

kingdaro
kingdaro

Reputation: 12028

The AngularFireList type expects a single type for what the list will contain, not an array type. The correct declaration for the employees array would be employees: AngularFireList<Employee> (not Employee[], not any[]).

The reason it's failing now is because it's expecting you to pass in something of type any[] to .push(), and Employee is not assignable to any[]

Upvotes: 1

Related Questions