I have a problem like this. I am developing a simple angular app. I am retrieving data from the database. For that, I have created a service.ts file like this.
import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import { Observable } from 'rxjs/Observable';
import 'rxjs/add/operator/map';
import 'rxjs/add/operator/toPromise';
import { Employee } from './employee';
@Injectable()
export class EmployeeService {
selectedEmployee: Employee;
public employees: Employee[];
readonly baseURL = 'http://localhost:3000/employees';
constructor(private http: HttpClient) { }
postEmployee(employee: Employee) {
return this.http.post(this.baseURL, employee);
}
getEmployeeList() {
return this.http.get(this.baseURL);
}
}
And in the components file, I have coded like this to call this function in the service.ts file.
import { Component, OnInit } from '@angular/core';
import { NgForm } from '@angular/forms';
import { EmployeeService } from '../shared/employee.service';
import { Employee } from '../shared/employee';
declare var M: any;
@Component({
selector: 'app-employee',
templateUrl: './employee.component.html',
styleUrls: ['./employee.component.css'],
providers: [EmployeeService]
})
export class EmployeeComponent implements OnInit {
constructor(private employeeService: EmployeeService) { }
ngOnInit() {
this.resetForm();
this.refreshEmployeeList();
}
refreshEmployeeList() {
this.employeeService.getEmployeeList().subscribe(function (res) {
this.employeeService.employees = res as Employee[];
});
}
resetForm(form?: NgForm) {
if (form) {
form.reset();
this.employeeService.selectedEmployee = {
_id: '',
name: '',
position: '',
office: '',
salary: null
};
}
}
onSubmit(form: NgForm) {
if (form.value._id === '') {
this.employeeService.postEmployee(form.value).subscribe((res) => {
this.resetForm(form);
M.toast({ html: 'Saved successfully', classes: 'rounded' });
});
}
}
}
If I replace
this.employeeService.employees = res as Employee[];
This as
console.log(res);
It prints the all the data in the database correctly in the console window but with this.employeeService.employees = res as Employee[] line it is giving me an error like this.
core.js:1449 ERROR TypeError: Cannot set property 'employees' of undefined at SafeSubscriber.eval [as _next] (employee.component.ts:26) at SafeSubscriber.__tryOrUnsub (Subscriber.js:243) at SafeSubscriber.next (Subscriber.js:190) at Subscriber._next (Subscriber.js:131) at Subscriber.next (Subscriber.js:95) at MapSubscriber._next (map.js:85) at MapSubscriber.Subscriber.next (Subscriber.js:95) at FilterSubscriber._next (filter.js:90) at FilterSubscriber.Subscriber.next (Subscriber.js:95) at MergeMapSubscriber.notifyNext (mergeMap.js:151)
This is my HTML file.
<div class="row">
<div class="col s12 m6">
<div class="card blue-grey darken-1">
<div class="card-content white-text">
<div class="row">
<div class="col s5">
</div>
<div class="col s7">
<table class="responsive-table highLight">
<thead>
<tr>
<th>Name</th>
<th>Position</th>
<th>Office</th>
</tr>
</thead>
<tr *ngFor="let emp of employees">
<td>{{emp.name}}</td>
<td>{{emp.position}}</td>
<td>{{emp.office}}</td>
</tr>
</table>
</div>
</div>
</div>
</div>
</div>
</div>
So what is the problem with my code?. Can someone help me to solve this problem?. I Look some examples, questions, and answers. They were not enough me to solve this problem. Thank You
ngFor = let emp of employeeService.employeesin your Template