[Solved] Server Error for queries even though I created an index for all attributes
- 0
- Databases
- Web
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:
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
what's yoru error?
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
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
Also, thanks for the tip about appending the queries to the query array depending on if the variables are defined or not
[Solved] Server Error for queries even though I created an index for all attributes
Recommended threads
- Seed db
hello there... is this correct way to seed appwrite
- Query Appwrite
Hello, I have a question regarding Queries in Appwrite. If I have a string "YYYY-MM", how can I query the $createdAt column to match this filter?
- Different appwrite IDs are getting expos...
File_URL_FORMAT= https://cloud.appwrite.io/v1/storage/buckets/[BUCKET_ID]/files/[FILE_ID]/preview?project=[PROJECT_ID] I'm trying to access files in my web app...