Every time I attempt to call getUsersChats again to display a badge for new messages, the code seems to be working fine. However, I encounter an error that says:
"ERROR TypeError: Cannot read property 'getUsersChat' of undefined"
getUsersChat function
getUsersChat() {
let clientsKeys: Array < any > ;
let i = 0;
// tslint:disable-next-line:prefer-const
let key: any;
this.mySegService.getChats().subscribe(res => {
this.clientsShortList = new Array < any > ();
clientsKeys = Object.keys(res);
this.totalNumberClients += clientsKeys.length;
clientsKeys.forEach(clientKey => {
if (i < ++i && res[clientKey]['messages'] !== undefined ) {
this.clientsShortList.push({
name: res[clientKey]['name'],
});
}
if (res[clientKey]['name'] === this.chatcheck ) {
this.clientsShortList = new Array < any > ();
this.clientsShortList.push({
name: res[clientKey]['name'],
number: this.compare
});
}
this.clientsShortList.reverse();
console.log(this.clientsShortList);
});
});
this.newmessageNumber(this.clientsShortList);
}
The issue arises in the setTimeout(() =>... part of the code. I'm unsure of what I'm doing wrong here, so any assistance would be greatly appreciated.
newmessageNumber(array: any) {
// tslint:disable-next-line:no-shadowed-variable
let element;
let i = 0;
for (let index = 0; index < array.length; index++) {
element = array[index]['name'];
}
this.mySegService.getChatsLast().subscribe(res => {
try {
localStorage.setItem('user', res[0]['user']);
} catch (error) {
// nd
}
this.chatcheck = localStorage.getItem('user');
// console.log(this.chatcheck);
if (this.chatcheck === element ) {
this.compare = '1';
}
});
for ( i ; i < 3; i++) {
(function(i) {
setTimeout(() => {
this.getUsersChat();
}, 1000 * i);
})(i);
}
}
If anyone can provide some guidance on resolving this issue, it would be much appreciated.