I am trying to push values dynamically to my data member inside the constructor, but inside the forEach block, I'm not able to access that member.
Here AppComponentClass
export class AvailableRidersComponent {
public availableDrones: Pickdrone[];
constructor() {
const db = firebase.database();
const availableRidersRef = db.ref('/riders/active_riders/available/');
availableRidersRef.on('value', snapshot => {
snapshot.forEach(function (childSnapshot) {
const riderProfileRef = db.ref('/riders/Pickdrones/' + childSnapshot.key);
riderProfileRef.on('value', dataSnap => {
const riderProfile = {
name: dataSnap.val().name,
id: dataSnap.key,
contact: dataSnap.val().contact,
email: dataSnap.val().email
};
console.log(riderProfile); //Prints values properly
this.availableDrones.push(riderProfile); //ERROR TypeError: Cannot read property 'availableDrones' of undefined
});
});
});
}
}
and here us my Pickdrone class
export class Pickdrone {
public name: string;
public id: string;
public contact: string;
public email: string;
// vehicleType: string;
constructor(name: string, id: string, contact: string, email: string){
this.name = name;
this.contact = contact;
this.id = id;
this.email = email;
}
}