Cannot read property 'Items' of null


#1

I am trying to push some data in to Items array but I am getting this error.

here is my code

import { Component,Inject } from ‘@angular/core’;
import { FirebaseApp } from ‘angularfire2’;
import { NavController,AlertController } from ‘ionic-angular’;
import { BarcodeScanner } from ‘ionic-native’;

@Component({
selector: ‘page-home’,
templateUrl: ‘home.html’
})
export class HomePage {

data: any;
id: any = 65584;
Items: any;

constructor(public navCtrl: NavController, @Inject(FirebaseApp) firebaseApp: any, public alertCtrl: AlertController) {
this.data = firebaseApp.database();
this.Items = [];
}

public add(){
var ref = this.data.ref(“Products/” + this.id);
ref.on(“value”, function(snapshot){
console.log(snapshot.val());
let ItemsObj = {
barcode: snapshot.val().barcode,
name: snapshot.val().name,
price: snapshot.val().price
};
this.Items.push(ItemsObj);
console.log(‘succes’);
console.log(this.Items);
}),function(error){
let alert = this.alertCtrl.create({
title: ‘Failed.’,
subTitle: error.message,
buttons: [‘OK’]
});
alert.present();
}
}

}


#2

Use fat arrow functions instead of function. Your execution context is unreliable. See here for details.


#3

Can you please help me how to use that fat arrow function in my case