I am currently working on passing an array from service.ts to a component. My goal is to display the array elements in a dialog box. However, I encountered a Typescript error
TypeError: Cannot read property 'departmentArr' of undefined
.
I am struggling to comprehend why departmentArr is being identified as undefined.
Within the service:
private todos: Todo[] = [];
private db: any;
public departmentArr: any = [];
async searchDepartments(): Promise<Observable<any>> {
const fetchData3 = await this.db.todos.orderBy('department').keys(function (departments) {
alert("Departments are: " + departments.join(','));
this.departmentArr.push(departments.join(','));
});
return this.departmentArr;
}
In the Component, I am attempting to retrieve the array from departmentArr
.
connection: any;
async showTable() {
this.connection = (await this.todoService.searchDepartments()).subscribe(message => {
//the message here signifies the array
console.log(message);
this.dialog.open(AppComponent, {
disableClose: true,
autoFocus: true,
data: message
});
});
}
Where could my mistake lie?