Closed Elbanteny closed 1 month ago
To handle the error where patient.name is null, you can add a null check before accessing the name property. This way, if patient is null or undefined, your code will not throw an error. To also handle the possibility that appointment.patient may be null or undefined when rendering the AppointmentModal, you can add a conditional check to ensure that appointment.patient exists before attempting to access its properties. Here's the whole modified code
`"use client";
import { ColumnDef } from "@tanstack/react-table"; import Image from "next/image";
import { Doctors } from "@/constants"; import { formatDateTime } from "@/lib/utils"; import { Appointment } from "@/types/appwrite.types";
import { AppointmentModal } from "../AppointmentModal"; import { StatusBadge } from "../StatusBadge";
export const columns: ColumnDef {row.index + 1} {patientName}
);
},
}, { accessorKey: "schedule", header: "Appointment", cell: ({ row }) => { const appointment = row.original; return (
{formatDateTime(appointment.schedule).dateTime}
);
},
}, { accessorKey: "primaryPhysician", header: "Doctor", cell: ({ row }) => { const appointment = row.original;
const doctor = Doctors.find(
(doctor) => doctor.name === appointment.primaryPhysician
);
return (
<div className="flex items-center gap-3">
<Image
src={doctor?.image!}
alt="doctor"
width={100}
height={100}
className="size-8"
/>
<p className="whitespace-nowrap">Dr. {doctor?.name}</p>
</div>
);
},
}, { id: "actions", header: () =>
return (
<div className="flex gap-1">
{appointment.patient ?(
<>
<AppointmentModal
patientId={appointment.patient.$id}
userId={appointment.userId}
appointment={appointment}
type="schedule"
title="Schedule Appointment"
description="Please confirm the following details to schedule."
/>
<AppointmentModal
patientId={appointment.patient.$id}
userId={appointment.userId}
appointment={appointment}
type="cancel"
title="Cancel Appointment"
description="Are you sure you want to cancel your appointment?"
/>
</>
): (
<p className="text-14-medium">No patient info</p>
)
}
</div>
);
},
}, ];`
Changes Made: Patient Column: Added a null check for appointment.patient?.name.
Doctor Column: Added a check to handle cases where the doctor might not be found (doctor ? ... : ...).
Actions Column: Added a check to ensure appointment.patient exists before rendering the AppointmentModal components. These changes will ensure that your code handles cases where some data might be missing, preventing runtime errors.
Thanks sir, its solved!
I just found out, why patient.name is null, the relationship in the database between appointment and patient does not seem to work properly, I have to defined it manually.
![Uploading Screenshot from 2024-07-23 21-55-36.png…]()
This my columns.tsx code
this my page.tsx in admin folder code: