During the demonstration, I declared the following in my app.component.ts just before the constructor
private testObjArray = []
In the ngOnInit method, the following code is present:
ngOnInit() {
console.log('test 1', this.testObjArray);
this.testObjArray = [1];
console.log('test 2', this.testObjArray);
this.databaseService.getNotification()
.on('child_added', function (obj) {
console.log('test 3', this.testObjArray);
})
}
This snippet is from the database.service.ts file:
getNotificationsForCurrentUser() {
let currentUser = this.userService.getCurrentUser()
let ref = firebase.database().ref('notifications/')
return ref;
}
Even though the .on event seems to be triggered since there is data in the database, I'm facing an issue accessing properties declared at the beginning of the app. The output on the console for the above code is:
test 1 []
test 2 [1]
FIREBASE WARNING: Exception was thrown by user callback. TypeError: Cannot read property 'testObjArray' of null
EXCEPTION: Cannot read property 'testObjArray' of null
I am looking for a way to reference this.testObjArray inside the .getNotification().on function so that I can update my template accordingly.