Back

[SOLVED]Searching user list by using Query.search via sdk throws an error

  • 0
  • Self Hosted
  • Auth
  • General
zjjt
26 Apr, 2024, 13:07

Hi everyone, I have a function which takes two optional parameters username and label and returns a list of users. It runs fine when i pass nothing to it and returns the whole list of my users. However when i pass a letter to the username parameter, it fails with server error. Below is the python code i am using to retreive the users:```def fetch_taggable_users(context, client): try: users = Users(client) # Parse the request body request_body = json.loads(context.req.body) if context.req.body else {} context.log(json.dumps(request_body, indent=4)) username = request_body.get('username', '') label = request_body.get('label', '')

TypeScript
    # Construct the query based on the request parameters
    query = [Query.equal('emailVerification', True), Query.equal('status', True)]
    if username:
        context.log(f"username is present {username}")
        query.append(Query.search('name', username))
    if label:
        context.log(f"label is present {label}")
        query.append(Query.equal('label', label))

    # Retrieve the list of users
    users_list = users.list(queries=query)
    simplified_users = [{'$id': user['$id'], 'name': user['name']} for user in users_list['users']]

    # Log and return the simplified user list
    context.log(json.dumps(simplified_users, indent=4))
    return context.res.json(simplified_users)
except Exception as e:
    # Log the error and return a 500 server error response
    error_message = f"An error occurred: {str(e)}"
    context.log(error_message)
    return context.res.json({'error': error_message})``` As you can see i am using Query.search to retrieve the list of users which will be used on the frontend to implement a mention system. I dont know how i can create an index for fulltext search on the name attribute of Users...
TL;DR
Issue: A developer was facing an error when using Query.search via SDK to search the user list. When passing a letter to the username parameter, it failed with a server error. However, the developer found a solution by utilizing the search parameter on the users.list function to achieve the desired results. Solution: Use the search parameter on the users.list function instead of Query.search to search for users effectively based on username.
Ernest
26 Apr, 2024, 13:52

First to make sure you're using the right sdk, what's your self-hosted appwrite version and the SDK version you're using?

zjjt
26 Apr, 2024, 13:54

@Ernest i am using appwrite 1.5.4 and the python sdk i dint specify a version in the requirement file.

zjjt
26 Apr, 2024, 13:55

But i seem to have figured it out, ther is a search parameter on the users.list function . When i use that i get the proper result i am looking for

zjjt
26 Apr, 2024, 13:55

ill mark this as solved...Thanks for helping out

zjjt
26 Apr, 2024, 13:56

[SOLVED]Searching user list by using Query.search via sdk throws an error

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