I have a model named Chat, defined as follows:
export class Chat {
chatId?: number;
chatUser1?: string;
chatUser2?: string;
chatStatus?: string;
created_at?: any;
updated_at?: any;
deleted_at?: any;
}
In the component, I need to retrieve the value of chatUser2 with a specific criteria. To achieve this, I created an array named chats for instances of the Chat model.
chats: Chat[];
Then, I implemented the following function:
getChats(): void {
this.loadingChats = true;
this.chatService.getChats().subscribe((data: any[]) => {
this.chats = data;
});
this.toCall = true;
this.toAnswer = false;
this.toHangUp = false;
var status = "true";
var chatUser2 = [];
var chatStatus = [];
var chatChannel = [];
chatUser2 = this.chats.map((v) => v.chatUser2);
chatStatus = this.chats.map((v) => v.chatStatus);
chatChannel = this.chats.map((v) => v.chatId.toString());
var size = chatUser2.length;
for (var i = 0; i < size; i++)
{
console.log(i.toString());
if (chatUser2[i] === this.userName && chatStatus[i] === status)
{
this.toAnswer = true;
this.toCall = false;
this.toHangUp = false;
this.channel = chatChannel[i];
console.log("break");
break;
}
}
However, an error is being displayed in the console - ERROR TypeError: "this.chats is undefined" at :
chatUser2 = this.chats.map((v) => v.chatUser2);
Your assistance is greatly appreciated.