Reputation: 24
When i am trying to save form data to firebase i am getting this error , i am new to angular & firebase . Below is the code .
This is my employee service class
employee.service.ts
import { Injectable } from '@angular/core';
import { AngularFireDatabase, AngularFireList } from 'angularfire2/database'
import { Employee } from '../../../models/employee.model';
@Injectable()
export class EmployeeService {
employeeList: AngularFireList<any>;
selectedEmployee: Employee = new Employee();
constructor(private firebase : AngularFireDatabase ) { }
getData(){
this.employeeList = this.firebase.list('employees');
return this.employeeList;
}
insertEmployee(employee : Employee)
{
this.employeeList.push({
name: employee.name,
gender: employee.gender,
email: employee.email,
phoneNumber: employee.phoneNumber,
contactPreference: employee.contactPreference,
dateOfBirth: employee.dateOfBirth,
department: employee.department,
isActive: employee.isActive,
photoPath: employee.photoPath
});
}
updateEmployee(employee : Employee){
this.employeeList.update(employee.$key,
{
name: employee.name,
gender: employee.gender,
email: employee.email,
phoneNumber: employee.phoneNumber,
contactPreference: employee.contactPreference,
dateOfBirth: employee.dateOfBirth,
department: employee.department,
isActive: employee.isActive,
photoPath:employee.photoPath
});
}
deleteEmployee($key : string){
this.employeeList.remove($key);
}
}
console error
Upvotes: 0
Views: 457
Reputation: 222722
It's happening because your employeeList is undefined when you try to push, just add this piece of code, that would initialize with your employee values
insertEmployee(employee : Employee)
{
if(!this.employeeList){
this.employeeList = this.getData();
}
this.employeeList.push({
name: employee.name,
email: employee.email
});
}
Upvotes: 2
Reputation: 129
this.firebase.list('employees') return undefined
Try this:
getData(){
this.employeeList = this.firebase.list('employees') || [];// default arr
return this.employeeList;
}
Upvotes: 2