Back

[SOLVED] Trying to store user preferences when creating an account

  • 1
  • Flutter
  • Web
  • Databases
  • Accounts
  • Users
tahakhawaja
29 Aug, 2023, 07:34

I get an error message when creating an account and attempting to store the role and user id in a database collection. What am i doing wrong in my code? These are the errors I get in the front-end console:

errors:

TypeScript
main.dart.js:45231     POST http://178.128.196.153/v1/databases/64e70d5fb77f8c332745/collections/64e70d954009050a62ec/documents 400 (Bad Request)

"invalid document structure: Missing required attribute: userId"

code:

TypeScript
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("Created user ID: ${user.$id}");

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

    return "success";
    } on AppwriteException catch(e) {
      return e.message.toString();
    }

}
TL;DR
The user is trying to store user preferences when creating an account. They are getting an error message "invalid document structure: Missing required attribute: userId". They are unsure of what they are doing wrong in their code. The solution is to change 'userId' to 'userid' in the code to match the attribute name.
Binyamin
29 Aug, 2023, 16:18

What is the collection structure?

Binyamin
29 Aug, 2023, 16:18

Attributes

tahakhawaja
29 Aug, 2023, 16:22

its the user id, and a role which can take the value "entrepreneur" or "coach"

Binyamin
29 Aug, 2023, 16:24

Try to log this before sending

TypeScript
 data: {
      'userId': user.$id,
      'role': role
    })

To see that none of these two values are empty, as if it will be send empty and one of them is required then it will be considered as Missing required attribute

tahakhawaja
29 Aug, 2023, 16:32

So I currently get this error:

"invalid document structure: Missing required attribute: userId"

Am i doing something wrong for the user id?

Binyamin
29 Aug, 2023, 16:34

Can you share screenshot of the attribute screen?

tahakhawaja
29 Aug, 2023, 23:41

This is how it looks

D5
29 Aug, 2023, 23:54

userid or userId?

tahakhawaja
29 Aug, 2023, 23:54

userid

D5
29 Aug, 2023, 23:55

Seems like in the create document part/function you're trying to save an attribute that doesn't exists called userId, because your attribute seen in the screenshot is "userid" with the I not in caps letters

tahakhawaja
29 Aug, 2023, 23:57

How would you suggest I fix this in order to store the attribute? Is there any part of the code I should correct?

D5
29 Aug, 2023, 23:58

Just put the i not in capital letters or change the attribute name by deleting it and adding it again (note, previous data will be lost)

tahakhawaja
29 Aug, 2023, 23:59

Are you referring to this, and saying make the capital I an i instead?

TypeScript
'userId': user.$id,
D5
30 Aug, 2023, 00:00

This is how will look your code after being modified:

TypeScript

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("Created user ID: ${user.$id}");


  // storing the user role in the

//Here the I should not be in caps, fixed in the code below


 database after successful registration
  await databases.createDocument(
    databaseId: '64e70d5fb77f8c332745',
    collectionId: '64e70d954009050a62ec',
    documentId: user.$id,
    data: {
      'userid': user.$id,
      'role': role
    });

    return "success";
    } on AppwriteException catch(e) {
      return e.message.toString();
    }

}
D5
30 Aug, 2023, 00:01

Yes

tahakhawaja
30 Aug, 2023, 00:02

thank you! i'll give this a try. appreciate your help!

D5
30 Aug, 2023, 00:02

Confirm me if it works to mark this as solved πŸ˜„

tahakhawaja
30 Aug, 2023, 01:08

It works! Thanks again!!

Binyamin
30 Aug, 2023, 01:08

<a:agooglethumbsup:635256484682530825>

Drake
30 Aug, 2023, 01:40

[SOLVED] Trying to store user preferences when creating an account

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