Skip to content
Init is coming / May 19 - 23
Back

Relationship data is returning null

  • 0
  • Databases
  • Web
Owa
28 Feb, 2024, 14:02

I'm not sure if this was already asked here, I cannot find the same issue I'm having. So here it is.

Currently, I have two collections with relationship Entry <-- Category, when I'm trying to fetch the data of the Entry the Category data I needed returns to me is null.

TypeScript
export async function getEntries() {
  const { documents } = await databases.listDocuments(
    process.env.NEXT_PUBLIC_APPWRITE_JOURNAL_DATABASE_ID as string,
    process.env.NEXT_PUBLIC_APPWRITE_JOURNAL_COLLECTION_ID_ENTRY as string
  );
  return {
    entries: documents.map(mapDocumentToEntries),
  };
}

I'm thinking maybe, I need to use the Collection_ID for the Category collection. But the API only accepts one Collection_ID

TL;DR
Issue: Relationship data returning null when fetching entries with corresponding categories data. Solution: Ensure to set up the relationship between `Entry` and `Category` as "many to one" to have the categories data populated in entries. Additionally, when fetching entries, use the correct `Collection_ID` for the Category collection to get the desired data.
Ernest
28 Feb, 2024, 14:05

What type of relationship did you create; 1-way or 2-way?

Owa
28 Feb, 2024, 14:10

One way. and set it to "many to one"

Kenny
28 Feb, 2024, 14:50

If it's one way from Category to Entries, then entries won't have categories data, but categories will have entries data

Owa
28 Feb, 2024, 14:54

this how I set it up. The Entries/Journal have the attribute that I got from Category.

Owa
28 Feb, 2024, 14:59

found the issue. 🤦

Kenny
28 Feb, 2024, 14:59

What was it? :)

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