Skip to content
Back

fetching from database

  • 1
  • Flutter
Joshima
30 May, 2023, 03:11

i want to fetch user followers from database and i double checked everything but still not able to get the list. here's my user_api code . there could be a mistake in query i guess -

TypeScript
  Future<List<model.Document>> getFollowers(String uid) async {
    final documents = await _db.listDocuments(
      databaseId: AppConstants.APPWRITE_DATABASE_ID, 
      collectionId: AppConstants.APPWRITE_USERS_COLLECIION,
      queries: [
        Query.equal('followers', uid)
      ]
    );
    return documents.documents;
  }```
TL;DR
The user is trying to fetch user followers from a database but is not getting the desired results. They are using the query `Query.equal('followers', uid)` to search for followers but it is not working. The user also mentioned that the followers field is a list. It was suggested to use `Query.search` or filter the results on the client side. The user also mentioned that when they try to print `followers.length`, it returns 0. The issue may be related to not creating an index on the `followers` field.
Asutosh
30 May, 2023, 05:33

it looks good, have you created an index on followers field?

Asutosh
30 May, 2023, 05:34

and could you pls send the error message

Joshima
30 May, 2023, 05:40

here's my controller code - Future<List<UserModel>> getFollowers(String uid) async { final followersDocs = await _userAPI.getFollowers(uid); return followersDocs.map((e) => UserModel.fromMap(e.data)).toList(); }

Joshima
30 May, 2023, 05:40

provider - final getFollowersProvider = FutureProvider.family((ref, String uid) async{ final UserProfileController = ref.watch(userProfileControllerProvider.notifier); return UserProfileController.getFollowers(uid); });

Joshima
30 May, 2023, 05:41

and here i am displaying - ref.watch(getFollowersProvider(user.uid)).when( data: (followers) { print('Followers Count: ${followers.length}'); print('User UID: ${user.uid}'); return ListView.builder( itemCount: followers.length, itemBuilder: (BuildContext context, int index) { final follower = followers[index]; return SearchTile(userModel: follower); }, ); }, error: (error, st) => ErrorText(error: error.toString()), loading: () => const Loader(), ),

Joshima
30 May, 2023, 05:41

followers present in my usermodel - class UserModel { final String email; final String name; final List<String> followers;

Joshima
30 May, 2023, 05:42

and there's no error. the list is just not displaying. and printing followers.length = 0

Joshima
30 May, 2023, 06:06

I think i am using wrong query because the followers are in list but idk what would be the right query to use

Asutosh
30 May, 2023, 06:27

as here followers field is a list its still wip here you can see an issue about the same https://github.com/appwrite/appwrite/issues/2838

Asutosh
30 May, 2023, 06:30

you can trying using Query.search (as mention in one of the comments it kinda works) or filter in the client side as per the requirment

Joshima
30 May, 2023, 08:35

i tried using query.search. still not working :_

Pradhumansinh Padhiyar
30 May, 2023, 08:40

Yes i tried using listDocument its not working, also not getting desired attribute data

Asutosh
30 May, 2023, 08:46

rn something like Query.contains is not present to search from a list but you can do filter it in the client side 😁

Asutosh
30 May, 2023, 08:46

whats exactly not working for you @Pradhumansinh Padhiyar

Pradhumansinh Padhiyar
30 May, 2023, 08:49

Something like

SELECT username, email, password, firstname, lastname from tbl_users WHERE lastname = "Ranjan"

How can i achieve something like this using Db collection ?

Asutosh
30 May, 2023, 08:57

add a query as Query.equal('lastname', 'Ranjan') and make a index for the lastname parameter in the appwrite console

Joshima
30 May, 2023, 08:58

how could i do that?

Asutosh
30 May, 2023, 09:00

actually depends on what exactly you are tying to do

Nikunj Panchal 🌿
30 May, 2023, 09:28

Some days Ago I am also Facing this issue You need To Specify your attribute into the indexes Section

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