Back

Appwrite Database Query

  • 0
  • Databases
  • Web
Chidera Humphrey
17 Jan, 2025, 02:35

I'm trying to filter documents in my database based on whether a user's email is in the shared_with array field.

Here's the error I am getting: Uncaught TypeError: file2.role.includes is not a function

And here's my code:

TypeScript
export const fetchMetadata = async (userId: string) => {
  try {
    // query database for files owned by the user
    const ownedFilesMetadata = await database.listDocuments(import.meta.env.VITE_DATABASE_ID, import.meta.env.VITE_FILE_METADATA_COLLECTION_ID, [Query.equal("ownerId", userId)]);

    // query database for files shared with the user
    const sharedFilesMetadata = await database.listDocuments(import.meta.env.VITE_DATABASE_ID, import.meta.env.VITE_FILE_METADATA_COLLECTION_ID, [Query.search("shared_with", userId)]);

    return {
      ownedFilesMetadata: ownedFilesMetadata.documents,
      sharedFilesMetadata: sharedFilesMetadata.documents
    }
  } catch (error) {
    console.log(error);
    return {
      ownFilesMetadata: [],
      sharedFilesMetadata: [],
    }
  }
}

What do you think I'm doing wrong?

TL;DR
Issue: Getting a `Uncaught TypeError: file2.role.includes is not a function` error when filtering documents based on whether a user's email is in a specific array field. Solution: The error might be due to trying to use the `includes()` method on a non-array field. Make sure `file2.role` is an array before calling `includes()` on 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