adrianhajdin / healthcare

Build a healthcare platform that streamlines patient registration, appointment scheduling, and medical records, and learn to implement complex forms and SMS notifications.
https://jsmastery.pro
1.68k stars 386 forks source link

Invalid relationship value. Must be either an array of documents or document IDs, document ID given occur during apointment creation #18

Closed saqlainzaheer closed 1 month ago

saqlainzaheer commented 1 month ago

Issue Description: Encountered "Invalid relationship value" error when creating an appointment using the createDocument method in the Appwrite SDK. CODE:

`export const createAppointment = async (
  appointment: CreateAppointmentParams
) => {
      console.log(appointment);

  try {
    const newAppointment = await databases.createDocument(
      DATABASE_ID!,
      APPOINTMENT_COLLECTION_ID!,
      ID.unique(),
      appointment

    );

    revalidatePath("/admin");
    return parseStringify(newAppointment);
  } catch (error) {
    console.error("An error occurred while creating a new appointment:", error);
  }
};`

ERROR Screenshot 2024-07-11 182921

Expected Behavior:

I expected the SDK to handle a single document ID correctly in the patientId field and create the appointment

Steps to Reproduce: Define a CreateAppointmentParams object with a patientId field that is expected to be an array of strings. Call createDocument with this object, passing a single string instead of an array of strings for patientId. Observe the error thrown by the SDK. CORECT CODE SHUOLD BE

`export const createAppointment = async (
  appointment: CreateAppointmentParams
) => {
      console.log(appointment);

  try {
    const newAppointment = await databases.createDocument(
      DATABASE_ID!,
      APPOINTMENT_COLLECTION_ID!,
      ID.unique(),
      {
        ...appointment,
        patient:[appointment.patient]

      }
    );

    revalidatePath("/admin");
    return parseStringify(newAppointment);
  } catch (error) {
    console.error("An error occurred while creating a new appointment:", error);
  }
};`
bharatpaliwal-169 commented 1 month ago

Hey @saqlainzaheer check your patient schema and the data-type assigned to these parameters. I problem remains unsolved, delete these params and create them again. Also, check the appointment schema properly from the video that's the only tricky part.

saqlainzaheer commented 1 month ago

The issue isn't related to the schema, as the schema is correct and patients register successfully. However, the problem arises on the appointment page when creating an appointment.

In the createAppointment function, I used the following approach:

{
  ...appointment,
  patient: [appointment.patient]
}

instead of this

const newAppointment = await databases.createDocument(
  DATABASE_ID!,
  APPOINTMENT_COLLECTION_ID!,
  ID.unique(),
  **appointment**
);

revalidatePath("/admin");
return parseStringify(newAppointment);

The reason for this change is that the relationship is one-to-many, so it requires an array of patient IDs rather than a single patient ID.

OmJadhav1 commented 1 month ago

relation is many to one . appointment can contain one patient patient can belong to many appointment

saqlainzaheer commented 1 month ago

Oh, yes, you are right. I made the relationship one-to-many instead of many-to-one. Thanks for the clarification.