Back

[SOLVED] Invalid document structure: Unknown attribute: "$collectionId"

  • 1
  • Functions
  • Web
huylv
3 Mar, 2023, 15:18

Hi, I got this error on Appwrite Function, but can't found any solution on google:

TypeScript
module.exports = async (req, res) => {
  const database = new sdk.Databases(client);
  const currentOrder = JSON.parse(req.variables.APPWRITE_FUNCTION_EVENT_DATA);
  const res1 = await database.updateDocument(
    currentOrder.$databaseId,
    currentOrder.$collectionId,
    currentOrder.$id,
    currentOrder,
    [
      sdk.Permission.read(
        sdk.Role.user(req.variables.APPWRITE_FUNCTION_USER_ID)
      ),
      sdk.Permission.update(sdk.Role.team("admin")),
    ]
  );
}
TL;DR
The user encountered an error "Invalid document structure: Unknown attribute: "$collectionId"". After some discussion, it was determined that the issue was with the payload of the event. The solution was to pass a new `currentOrder` as the third parameter in the `updateDocument` function and only include the data that needed to be updated. The user also mentioned that the error logs from Appwrite's functions are not good and suggested improvements in that area.
joeyouss
3 Mar, 2023, 15:22

Hi there 👋 allow me to look into this and get back to you

huylv
3 Mar, 2023, 15:38

hope to see your response soon 😄

joeyouss
3 Mar, 2023, 15:57

So collectionId should not be in currentOrder, only in parameter should be fine

huylv
4 Mar, 2023, 00:57

But when I console.log(currentOrder.$collectionId), it show correct collection id

huylv
4 Mar, 2023, 05:48

Please help me @joeyouss

joeyouss
4 Mar, 2023, 06:11

Hi @huylv , just DMed you

huylv
5 Mar, 2023, 01:00

still need help 😦

Meldiron
5 Mar, 2023, 07:59

Hey there 👋 Problem here seems to be that event give you whole order document, but updateDocument doesn't like that you provide all those attributes in the document. In the end, update endpoint only needs stuff that you want to update.

Soo solution here should be to pass new currentOrder as third parameter, and make it only have data that you want to update.

Tho I think this should work - this might be bug with payload of specific event. Ill take a look to double check.

Meldiron
5 Mar, 2023, 08:04

Indeed, I am getting same error when trying to reproduce. This code fails:

TypeScript
    editHabitName: async (habit: Habit, newName: string) => {
        habit.name = newName;
        return await databases.updateDocument<Habit>('main', 'habits', habit.$id, habit);
    },

This code works:

TypeScript
    editHabitName: async (habitId: string, name: string) => {
        return await databases.updateDocument<Habit>('main', 'habits', habitId, {
            name
        });
    },

When doing updateDocument, make sure to pass data object that only has stuff you want to change. In my scenario, the name attribute.

huylv
5 Mar, 2023, 12:57

@Meldiron Thanks, I have done it. Just pass empty object to the third parameter and only update the permission. Appwrite is really awesome, but error logs from appwrite's functions are not good. Hope you guys improve it 😄

Meldiron
5 Mar, 2023, 13:09

Eyyy glad you solved it ✨

cc @VincentGe regarding dev-experience with errors

Meldiron
5 Mar, 2023, 13:23

[SOLVED] Invalid document structure: Unknown attribute: "$collectionId"

VincentGe
6 Mar, 2023, 23:24

Ooof this is a hard one. 👀 We can probably write these better for the update/create endpoints though.

Drake
6 Mar, 2023, 23:32
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