Back

[SOLVED] always getting `Document with the requested ID already exists.' in cloud function

  • 0
  • Databases
  • Functions
Mosh Ontong
2 Dec, 2023, 18:59
TypeScript
 const {
    clinicRef,
    patientRef,
    orgRef,
    type,
    startDate,
    endTime,
    endDate,
    startTime,
  }: CreateConsultationData = req.body;
  const databases = new Databases(client);
  const teams = new Teams(client);
  const functions = new Functions(client);

  const { "x-appwrite-user-id": userId, "user-id": backUp }: RequestHeaders =
    req.headers;

  const creator = userId || backUp;

  if (!creator) {
    return res.send(
      JSON.stringify({
        message: "either x-appwrite-user-id or user-id header is required",
      }),
      404,
      {
        "content-type": "application/json",
      }
    );
  }

  try {
    const response = await databases.createDocument(
      Bun.env.DATABASE_ID,
      Bun.env.CONSULTATION_COLLECTION_ID,
      ID.unique(),
      {
        orgRef,
        patientRef,
        patient: patientRef,
        clinicRef,
        facility: clinicRef,
        type,
        startDate,
        endDate,
        startTime,
        endTime,
      }
    );
   

    return res.send(
      JSON.stringify({
        message: "Consultation created",
        data: response,
      }),
      200,
      {
        "content-type": "application/json",
      }
    );
  } catch (e) {
    error(`Error creating facility: ${e.message}`);
    return res.send(
      JSON.stringify({
        message: `Error creating facility: ${e.message}`,
      }),
      500,
      {
        "content-type": "application/json",
      }
    );
  }

When I have to delete the existing document, this code above it will executed successfully, after the execution, now I am getting back the error of Document with the reuqested ID already exists

TL;DR
The user was getting the error "Document with the requested ID already exists" in their cloud function. They resolved the issue and thanked someone named Steven. The problem turned out to be with their relationships and payload. The code provided in the support thread is for creating a consultation document in a database and returning a response. The solution to the error is not explicitly mentioned in the thread.
Drake
2 Dec, 2023, 19:04

Do you have a unique index on the collection or any related collections?

Drake
2 Dec, 2023, 19:04

Is there relationships? That might be a problem too

Mosh Ontong
2 Dec, 2023, 19:05

I dont have unique index

Mosh Ontong
2 Dec, 2023, 19:06

I will check the relationship

Drake
2 Dec, 2023, 19:08

It could be trying to create a related document that already exists..what's your payload?

Mosh Ontong
2 Dec, 2023, 19:08

It seems the relationship

Mosh Ontong
2 Dec, 2023, 19:09
Mosh Ontong
2 Dec, 2023, 19:09

is my relation correct?

Drake
2 Dec, 2023, 19:10

Facility can only be linked to 1 consultation?

Mosh Ontong
2 Dec, 2023, 19:11

many consultation

Mosh Ontong
2 Dec, 2023, 19:11

ohh I see one to many should be

Drake
2 Dec, 2023, 19:11

Uhh many to one

Mosh Ontong
2 Dec, 2023, 19:11

ok I will try

Mosh Ontong
2 Dec, 2023, 19:16

thanks steven its now working

Drake
3 Dec, 2023, 00:39

[SOLVED] always getting `Document with the requested ID already exists.' in cloud function

Reply

Reply to this thread by joining our Discord

Reply on Discord

Need support?

Join our Discord

Get community support by joining our Discord server.

Join Discord

Get premium support

Join Appwrite Pro and get email support from our team.

Learn more