Nacim Idjakirene Nacim Idjakirene - 23 days ago 21
AngularJS Question

Angular 2 : cannot read property 'push' of undefined

In my Angular 2 application i have a function :

notification : Array<any>;
......
......
getNotification () {
return setTimeout(() => {
this.AppbankService.notify(this.user.NameId)
.subscribe(
(response) => {
if (response.status === 200) {
this.notifications.push(response.json());
console.log(typeof(this.notifications));
}
this.getNotification();
}
)
},5000)}


In this function i get notification from the server every 5 second, and try to push them to an array, but a have this : error app.module.ts:104 error : TypeError: Cannot read property 'push' of undefined(…)

Any suggestion ?

Answer

Change

notification : Array<any>;

to

notification : Array<any> = [];