I am getting an error when trying to delete object. Currently there is no data as it is added through out other code. May issue is that I may need to delete the data if it exist. I guest I need a way to check if the data exist before deleting
Error message: TypeError: Cannot convert undefined or null to object
The error is coming from this line: delete rooms?.[roomId]?.[socket.id];
interface IDevice {
deviceId: string;
deviceName?: string;
socketId: string;
isReconnecting?: true | false;
}
const rooms: Record<string, Record<string, IDevice>> = {};
if (typeof rooms?.[roomId]?.[socket.id] !== 'undefined' && rooms?.[roomId]?.[socket.id] !== null) {
delete rooms?.[roomId]?.[socket.id];
}
if, as you have already checked that the properties exist.delete nullanddelete undefinedwork fine for me.