Back

Getting error about duplicated document when adding new document.

  • 0
  • Databases
Nei
24 Oct, 2024, 20:01

Hi guys, I found another user with similar issue but he recreated the collection. I don't want to go this path since that is very time consuming.. my issue is that I can garantee that I am trying to create the documents with different keys using ID.unique() and it works only for the first document of my collection. if Itry a second document with a different id but and different unique field it does not work. I've tried to remove my unique key on my field cnpj and the error persist. Using nextjs.

TL;DR
Developers experiencing a duplicated document error when adding new documents. One user found a solution by creating documents with only two fields for testing. The error persisted despite using different IDs and unique fields. A potential solution suggested testing with another collection. Another user shared code snippets and logs showing different unique IDs, but the issue persisted. The problem seems to occur only after the first document in the collection. The user does not want to recreate the collection due to time constraints.
Nei
24 Oct, 2024, 20:03

this is my code:

TypeScript
  try {
    const incomingData = await req.json();

    incomingData.cnpj = parseInt(incomingData.cnpj, 10);

    const document = {
      ...defaultDocument,  // Default values
      ...incomingData      // Overwrites with the values from the request body
    };

    const document2 = {
      cnpj: document.cnpj,
      razao_social: document.razao_social
    }

    console.log(document);
    const uniqueId = ID.unique();
    console.log("Generated Unique ID:", uniqueId);

    const documentCreeated = await databases.createDocument(
      databaseId,
      collectionId,
      uniqueId,
      document2
    );

    return NextResponse.json({ success: true, documentCreeated });
  } catch (error) {
    return NextResponse.json({ success: false, error: error.message, details: error }, { status: 500 });
  }
Nei
24 Oct, 2024, 20:04

this my my two calls, the first called when collection is empty.

Axistro
24 Oct, 2024, 20:06

Did you imported ID from appwrite?

Nei
24 Oct, 2024, 20:07

Yes I did.

Nei
24 Oct, 2024, 20:07
TypeScript
 POST /api/cnpj 500 in 822ms
{
  cnpj: 12333,
  razao_social: null,
  email: null,
  nome_fantasia: null,
  ...
}
Generated Unique ID: 671aa813002c760d0676
 POST /api/cnpj 200 in 819ms
{
  cnpj: 1233399,
  razao_social: null,
  email: null,
  nome_fantasia: null,
  ...
}
Generated Unique ID: 671aa82600313179d157
 POST /api/cnpj 500 in 832ms
Nei
24 Oct, 2024, 20:08

this is my logging showing the id is different on each save

Nei
24 Oct, 2024, 20:09

that is so weird, even via cloud it error

Axistro
24 Oct, 2024, 20:10

This should be the case 🤔

Nei
24 Oct, 2024, 20:10

yeh at least that is consistent 🙂

Nei
24 Oct, 2024, 20:11

I have no indexes atm.

Axistro
24 Oct, 2024, 20:12

Mine works fine. Can you show the document that already exist?

Axistro
24 Oct, 2024, 20:12

Like ss

Nei
24 Oct, 2024, 20:12

you want to see the json?

Axistro
24 Oct, 2024, 20:13

No I wanted to see the id

Axistro
24 Oct, 2024, 20:14

Can you just try to test with another collection?
If the issue persist?

Nei
24 Oct, 2024, 20:15

I will create a new one, but it very time consuming to create collection thought the UI. 😦

Nei
24 Oct, 2024, 20:15

I will create with just two fields to test

Nei
24 Oct, 2024, 20:19

it did work now. . like the other user who had same issue.

Nei
24 Oct, 2024, 20:20

that seems like a bug somewhere

Axistro
24 Oct, 2024, 20:24

Hope i dont run into this issue in middle of development🙏

Nei
24 Oct, 2024, 20:26

lol me too. I am just starting using iti and if that breaks down the road will be painful

Nei
24 Oct, 2024, 20:26

anyway thanks for the help @Axistro

Axistro
24 Oct, 2024, 20:42

@Kenny

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