I want to push the values into a array like below when i retrieve values from firebase database
columns: any[];
this.columns = [
{ columnDef: 'FirstName', header: 'First Name', cell: (element: any) => `${element.FirstName}` },
{ columnDef: 'LastName', header: 'Last Name', cell: (element: any) => `${element.LastName}` },
];
Here is what i tried so far.........
this.af.list('/datalist', {
query: {
limitToLast: 200,
orderByChild: 'name',
equalTo: 'spiderman',
preserveSnapshot: true
}
}).subscribe(snapshot=>{
if(snapshot!=undefined){
snapshot.forEach(function(childSnapshot) {
this.columns.push( { columnDef: childSnapshot.heroname, header: childSnapshot.herotitle, cell: (element: any) => `${element.heroname}` });
this.displayedColumns = this.columns.map(c => c.columnDef);
return false;
});
}
});
Error with above code is Cannot read property 'columns' of undefined
Even though i declared the columns array globally its not recognizing it.
In HTML i want to use like this....
<ng-container *ngFor="let column of columns" [cdkColumnDef]="column.columnDef">
<mat-header-cell *cdkHeaderCellDef>{{ column.header }}</mat-header-cell>
<mat-cell *cdkCellDef="let row">{{ column.cell(row) }}</mat-cell>
</ng-container>
Any hint is appreciated. Thank you.