Back

Attempting to retrieve data from database collection

  • 0
  • Flutter
  • Web
  • Databases
  • Users
  • Self Hosted
tahakhawaja
29 Jan, 2024, 05:54

I'm attempting to retrieve data from a database collection which contains two attributes. A userid/documentid and a list of Type string [] topics. However, I currently get this error in the console. I am attempting to retrieve these and present them on my front-end. Is there something wrong with the way that I am retrieving these documents? I have stored the topics as a list of strings.

error:

TypeScript
main.dart.js:28183 User document data: {firstName: Jim, lastName: Halpert, userid: 65b73a56b64d1d9b0fd5, email: jim@gmail.com, $id: 65b73a56b64d1d9b0fd5, $createdAt: 2024-01-29T05:40:39.328+00:00, $updatedAt: 2024-01-29T05:40:39.328+00:00, $permissions: [], $databaseId: 65a6487dde332646854c, $collectionId: 65a64900d60baca39d1b}

main.dart.js:28183 Error fetching user preferences: NoSuchMethodError: method not found: 'gaf' on null

appwrite function:

TypeScript
// getUserPreferences function
Future<List<String>> getUserTopics(String userId) async {
  try {
    final userPreferencesDatabase = Databases(client);
    final preferencesDocument = await userPreferencesDatabase.getDocument(
      databaseId: '6581a7a98574997f7e84',
      collectionId: '6581a97c17588b1fb066',
      documentId: userId,
    );

    return List<String>.from(preferencesDocument.data['Topics']);
  } catch (e) {
    // Handle error
    print("Error fetching user preferences: $e");
    return [];
  }
}

appwrite function:

TypeScript
  Future<void> _updateUserTopics() async {
    String? userId = await getUserId();
    if (userId != null) {
      List<String> topics = await getUserTopics(userId);

      setState(() {
        _topicsController.text = topics.join(', ');
      });
    } else {
      print("User ID is null");
    }
  }
TL;DR
The developer is trying to retrieve data from a database collection but is encountering an error. They are storing the topics as a list of strings. The error suggests that there might be an issue with the way they are retrieving the documents. The provided code includes a function to fetch user preferences and an update function. The update function retrieves the user ID and calls the `getUserTopics` function to get the topics associated with the user. If successful, the topics are joined together and set in the `_topicsController`. If the user ID is null, it prints a message indicating so. Solution: The developer should check if the `getUserTopics` function
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