代码工作正常但是当我尝试再次调用 getUsersChats 时,如果有新消息要显示徽章,
我不能给我这个错误,
"ERROR TypeError: Cannot read property 'getUsersChat' of undefined"
getUsersChat函数
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);
}
这里是错误来自setTimeout(()=&gt; ...我在这里做错了什么,任何帮助都会很好
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);
}
}
有人可以帮我解决这个问题。
答案 0 :(得分:1)
因为 getUsersChat 不在setTimeout
尝试保留此范围,并像这样使用。
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);
}
}
答案 1 :(得分:0)
通过这样解决
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();
}
});
}