Back

AppwriteException: Invalid document structure - [For React Native]

  • 0
  • Databases
  • Web
0xabdulkhalid
23 Dec, 2023, 03:51

I'm trying to update the data which has been stored on a Collection in ENUM Type. I using the user id as document id during it's creation. I have no issues while creating and deleting it, but I'm facing issues when i tried to update the values.

This is the structure of data which has been stored,

TypeScript
{
    profile: {
      links: {
        github: "xxx",
        linkedin: "xxxx",
      },
      about: "", 
      points: 0, 
      courseStats: {},
    },
}

This is how i tried to update the document,

TypeScript
  const updateUserData = async (
    fieldToUpdate,
    dataToUpdate,
    isNested = false,
    fieldToInsert
  ) => {
    try {
      // Update document
      const updatedDocument = await databases.updateDocument(
        "DOCUMENT-ID",
        "COLLECTION-ID",
        user.$id, // Acts as DOCUMENT-ID
        isNested
          ? {
              profile: {
                ...userData,
                [fieldToUpdate]: {
                  ...userData[fieldToUpdate],
                  [fieldToInsert]: dataToUpdate,
                },
              },
            }
          : {
              profile: {
                ...userData,
                [fieldToUpdate]: dataToUpdate,
              },
            }
      );

      // Update state with the new document data
      setUserData(updatedDocument.profile);
    } catch (error) {
      console.error("Error updating document:", error);
    }
  };

After this function's execution, the backend only returns this error,

TypeScript
Error updating document: [AppwriteException: Invalid document structure: Attribute "profile" has invalid format. Value must be one of (links, about, points, courseStats)]
TL;DR
User is encountering an AppwriteException error while trying to update values in a Collection with nested data. They have no issues with create and delete operations. The error message states that the 'profile' attribute has an invalid format and it should only have values of 'links', 'about', 'points', or 'courseStats'. The user shares the structure of the data they are trying to update and provides the code they are using for the update. Solution: The user should check if they are updating the 'profile' attribute correctly and ensure that the value being passed is one of the allowed values. They can also inspect the network logs in their
Drake
23 Dec, 2023, 19:17

Uhh you can't nest data like that with an enum attribute...would you please share your collection as JSON? If you inspect the network logs in your browser while viewing the loading the collection in the Console, there should be an API call for the collection

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