代码运行良好,但当我再次尝试调用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(() =>)上的地方.我在这里做错了什么,任何帮助都会很好
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);
}
}有人能帮我解决这个问题吗。
发布于 2018-06-06 14:14:57
通过这样做来解决
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();
}
});
}发布于 2018-06-06 12:01:40
因为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);
}
}https://stackoverflow.com/questions/50719751
复制相似问题