I have here a class that builds a linkedlist for me to store users who are in a chat room for a small application. I have constructed the class to have an .toArray function because I am storing the array version on the frontend in React as of now. However when I call the function it produces this error: TypeError: roomLinkedList.toArray is not a function. I cannot seem to find a concrete explanation to fix the issue in the future. If there is something fundamentally wrong with the structure of the code, please feel me in. Thank you.
*NOTE: I did also create a separate type to try and see if that would help work instead of using the default class type and both do not work as well.
LinkedList Class
export class LinkedList {
head: Node;
tail: Tail;
length: number;
constructor(User: User) {
this.head = {
val: User,
next: null,
};
this.tail = this.head;
this.length = 1;
}
async add(member: User) {
const newMember: User = member;
newMember.position = ++this.length;
const Node: Node = { val: member, next: null };
this.tail!.next = Node;
this.tail = Node;
this.length++;
return 0;
}
async toArray() {
let arr: Array<User> = [],
i = 0;
while (i < this.length) {
if (this.head.val != null) {
const USER: User = {
id: this.head.val.id,
position: this.head.val.position,
username: this.head.val.username,
};
arr.push(USER);
}
i++;
}
return arr;
}
async remove(User: User) {
const parentNode = await this.get(User.position - 1);
return 0;
}
async get(Index: number) {
let count: number = 0;
let currNode: Node | null = this.head;
while (count < Index) {
currNode = currNode!.next;
count++;
}
return currNode as Node;
}
async exist(User: User) {
const node = await this.get(User.position);
if (User.id === node.val.id) return true;
return false;
}
}
LinkedList Type
import { User } from "./User";
export type Node = { val: User; next: Node | null };
export type Tail = Node | null;
export type LinkedList = {
head: Node;
tail: Tail;
length: number;
add(member: User): Promise<number>;
toArray(): Promise<User[]>;
remove(User: User): Promise<number>;
get(Index: number): Promise<Node>;
exist(User: User): Promise<boolean>;
};
Room Type
import { LinkedList } from "./LinkedList";
export type Room = {
key: string;
name: string;
maxCapacity: number;
members: LinkedList | null;
};
Area where logic is breaking
socket.on("join-room", async (data: JoinRoomDatagram) => {
const ROOM: Room = await _searchForRoom(data.roomID);
console.log(ROOM);
const roomLinkedList: LinkedList = ROOM.members as LinkedList;
console.log(roomLinkedList);
if (ROOM.name.trim().length > 0) {
if (roomLinkedList.length >= ROOM.maxCapacity) {
socket.emit("room-status", { msg: "full" });
return;
}
const Users: User[] = await roomLinkedList.toArray();
const foundUser = Users.find((User) => {
return User.username == data.user.username;
});
if (!foundUser) {
const newMember: User = {
id: socket.id,
position: 0,
username: data.user.username,
};
const result = await roomLinkedList.add(newMember);
if (result != 0) {
console.log("Error: Could not add member to list.");
return;
}
}
} else {
socket.emit("room-status", { msg: "error" });
return;
}
const roomArray = await roomLinkedList.toArray();
socket.emit("all-users", { users: roomArray });
socket.join(ROOM.key);
await _updateRoom(ROOM);
});