Back

Storing data in a database collection

  • 0
  • Users
  • Flutter
  • Databases
  • Web
tahakhawaja
9 Jan, 2024, 04:41

Hey everyone, i'm running into an issue when storing user data in a database collection. Currently, the function does not run or store any data in the collection. The way it works right now is a user first created an account, so I run the "createUser" function, and then in a subsequent webpage they select up to a total of 21 topics, which are added in a database collection associated with their user id. The function I run for this is "createUserTopics". Is there something wrong with the way I am handling my code?

TL;DR
The user is encountering an issue when storing user data in a database collection. The "createUser" function does not run or store any data in the collection. The user is also unsure if there is something wrong with the code. Another user suggests checking if the right permissions are granted. The user is asked for the error they are getting and where they are encountering it. Solution: It is recommended to check the permissions granted and to provide information about the specific error message and where it occurs.
tahakhawaja
9 Jan, 2024, 04:41
TypeScript
// Registering the User (Sign Up)
Future<String> createUser(String name, String email, String password, String role) async {
  try {

  final user = await account.create(
    userId: ID.unique(),
    email: email,
    password: password,
    name: name);
    print("New User created");
    print("Working so far");
    print("Created user ID: ${user.$id}");

  // storing the user role in the database after successful registration
  await databases.createDocument(
    databaseId: 'xxx',
    collectionId: 'xxx',
    documentId: user.$id,
    data: {
      'userid': user.$id,
      'role': role
    });

    // Automatically login user after account creation
    try {
      Map<String, dynamic> loginResult = await loginUser(email, password);
      bool loginSuccess = loginResult['success'];

      if (loginSuccess) {
        print("User automatically logged in after registration");
      } else {
        print("Failed to automatically log in");
      }
    } catch (e) {
      // Handle any exceptions thrown by loginUser
      if (e is AppwriteException) {
        print('Failed to automatically log in: ${e.message}');
      } else {
        print('An unknown error occurred while trying to log in: $e');
      }
    }

    return user.$id;
  } on AppwriteException catch(e) {
    return e.message.toString();
  }
}
TypeScript
// Function to update user preferences with selected topics
Future<String> createUserTopics(String userId, List<String> topics) async {
  try {
    // Update the document with the list of topics
    await databases.createDocument(
      databaseId: 'xxx',
      collectionId: 'xxx',
      documentId: userId,
      data: {
        'topics': topics
      },
    );

    print("User preferences updated successfully");
    return "User preferences updated successfully";
  } catch (e) {
    print("Error updating user preferences: $e");
    return "Error updating user preferences";
  }
}
ideclon
9 Jan, 2024, 07:03

What's the error you're getting, and where?

Ernest
9 Jan, 2024, 08:42

You should also check whether the right permissions are granted

tahakhawaja
10 Jan, 2024, 05:23

I'm not getting any error at all, which is why i'm a bit confused

ideclon
10 Jan, 2024, 05:23

Does the user get created?

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