Back

[Solved] Server Error for queries even though I created an index for all attributes

  • 0
  • Databases
  • Web
BI
21 Mar, 2023, 23:20

I am getting an Appwrite server error for my query even though I created an index that encompasses all the relevant attributes.

Here is the code:

TypeScript
  Query.orderDesc("$createdAt"), 
  Query.equal("subject", [subject]),
  Query.equal("education_level", [educationLevel]),
  Query.search("question", [searchTerm])
]  

const refresh = async () => {
  try {
    console.log(collectionId)
    const response = await databases.listDocuments(databaseId, collectionId, queryArray)  
    console.log(response);
    setNotes(response.documents);
    setLoading(false);       
  } catch (error) {  
    console.error(error);      
  }
};```

When I remove all the queries except for Query.orderDesc, it works fine. The subject, educationLevel and searchTerm are variables that are set by the parent component and passed down as props so they aren't always defined. The queries are meant to work like filters where if the user wants to filter by question, subject, educationLevel or all 3 they can. Do I need to change the queryArray for each of these scenarios so that only defined search variables are used in the query array? For example if the user only fills in the searchTerm variable, queryArray needs to be [
  Query.orderDesc("$createdAt"), 
  Query.search("question", [searchTerm])
] 

I tried the array above and it also worked after I added a question only index. Therefore, I'm not sure if I have to change queryArray for each scenario or not but it seems like I do. However, if I can avoid that and keep the code simple, that would be great
TL;DR
The user is experiencing a server error for their queries, even though they created an index for all attributes. They have a queryArray containing multiple queries, but the subject, educationLevel, and searchTerm variables are not always defined. The queries are meant to work like filters. The solution is to change the queryArray for each scenario so that only defined search variables are used in the query array. This can be done by adding a Query.equal() or Query.search() for the relevant variables. The user has tested removing the queries and it works fine, but when adding the queries back, it results in a server error. By adjusting the queryArray
Drake
21 Mar, 2023, 23:34

what's yoru error?

Drake
21 Mar, 2023, 23:36

Do I need to change the queryArray for each of these scenarios so that only defined search variables are used in the query array?

Yes. I would have an array of queries and then add a Query.equal() or whateveer for whatever the user selects

BI
21 Mar, 2023, 23:38

Oh, ok, thanks. I wasn't sure if it was another error or if it was my theory that every search variable needs to be defined before it is used for the queries. Beforehand, I'd assumed that undefined queries would be ignored and only the defined ones would be used to list the documents

BI
21 Mar, 2023, 23:39

Also, thanks for the tip about appending the queries to the query array depending on if the variables are defined or not

BI
21 Mar, 2023, 23:41

[Solved] Server Error for queries even though I created an index for all attributes

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