Reputation: 1076
The code is working fine but when i try to call getUsersChats again if there is a new message to display a badge,
i cant as it is giving me this error,
"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);
}
here is where the error comes on the setTimeout(() =>... what im doing wrong here, any help would be nice
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);
}
}
Can someone help me out on this.
Upvotes: 0
Views: 86
Reputation: 1076
solved by doing this
getUsersChat() {
let clientsKeys: Array < any > ;
let i = 0;
// tslint:disable-next-line:prefer-const
let key: any;
// tslint:disable-next-line:no-shadowed-variable
let element: 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: '1'
});
}
this.clientsShortList.reverse();
console.log(this.clientsShortList);
});
});
// this.newmessageNumber(this.clientsShortList);
for (let index = 0; index < this.clientsShortList.length; index++) {
element = this.clientsShortList[index]['name'];
}
this.mySegService.getChatsLast().subscribe(res => {
// console.log(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.getUsersChat();
}
});
}
Upvotes: 0
Reputation: 4700
because the getUsersChat is not in the this scope inside setTimeout
try preserving the scope of this and using like this.
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';
}
});
var self=this;
for ( i ; i < 3; i++) {
(function(i) {
setTimeout(() => {
self.getUsersChat();
}, 1000 * i);
})(i);
}
}
Upvotes: 1