Back

Need help with relationship issue

  • 0
  • Self Hosted
  • Web
m0hsin_
5 Feb, 2024, 12:16

I have two objects, a company object and a user object. I create a company first then I create a user for the company. The structure for the company is as follows

{ name: "", ...... users: [Object object] //Relationship with user object }

The user object is:

TypeScript
                    "id": userObject.$id,
                    "role": role,
                    "department": department,
                    "title": title,
                    "about": about
                }```

Now, I update the company with the following piece of code:

let payload = { "id": company.$id, "name": company.name, "location": company.location, "website": company.website, "users": [{ "id": userObject.$id, "role": role, "department": department, "title": title, "about": about }] }

TypeScript
    const updatedCompany = await database.updateDocument(
        process.env.REACT_APP_APPWRITE_DATABASE_ID,
        process.env.REACT_APP_APPWRITE_COMPANIES_COLLECTION_ID,
        company.$id,
        payload
    )```

Problem is when I run this code, I get the following error message: Message: Invalid document structure: Missing required attribute "role"

What could I be doing wrong?

TL;DR
Developer is encountering an error message "Invalid document structure: Missing required attribute 'role'" when trying to update a company using Appwrite. They have shared code snippets and screenshots of their company and user objects. They need assistance in identifying what they might be doing wrong. Solution: The error is caused by a missing required attribute "role". Check the code snippet provided and ensure that the "role" attribute is being passed correctly in the payload when updating the company.
kamal.panara
5 Feb, 2024, 12:18

can you share the screenshot of attributes of both collections?

m0hsin_
5 Feb, 2024, 12:20

@kamal.panara here is the company collections:

m0hsin_
5 Feb, 2024, 12:20

@kamal.panara here is the users object

m0hsin_
5 Feb, 2024, 12:21

Oh, and when I inspect the request from the networks tab, I can clearly see that I am sending the correct payload:

TypeScript
{
  "id": "65bd2e3105b07cd4346b",
  "name": "Shield",
  "location": "Nairobi, Kenya",
  "website": "https://shield.com",
  "users": [
    {
      "id": "65bd2e4a23d633740a80",
      "role": "admin",
      "department": "Mobamba",
      "title": "Mo",
      "about": "Mo"
    }
  ]
}
kamal.panara
5 Feb, 2024, 12:22

can you show users relationship with companies ? like click edit attribute and share the screenshot of the popup.

m0hsin_
5 Feb, 2024, 12:23

No worries, here you go @kamal.panara

kamal.panara
5 Feb, 2024, 12:25

can you try running your web app in new incognito tab ?

kamal.panara
5 Feb, 2024, 12:25

looks like browser cache issue

m0hsin_
5 Feb, 2024, 12:31

Thanks, but I am still getting the same error

kamal.panara
5 Feb, 2024, 12:36

since user can belong to only 1 company, can you also try passing companies attribute in user object:

TypeScript
{
    "id": company.$id,
    "name": company.name,
    "location": company.location, 
    "website": company.website,
    "users": [{
            "id": userObject.$id,
            "role": role,
            "department": department,
            "title": title,
            "about": about,
            "companies": company.$id,
        }]
}
kamal.panara
5 Feb, 2024, 12:38

if that also doesn't works, then some else will help you from the community, because I also think you are doing everything right.

and also since you are using Appwrite self-hosted, can you also mention the appwrite instance version?

m0hsin_
5 Feb, 2024, 12:50

Thank you @kamal.panara I am on appwrite 1.4.12, let me try out your suggestion

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