[SOLVED] Trying to store user preferences when creating an account
- 1
- Flutter
- Web
- Databases
- Accounts
- Users
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:
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:
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();
}
}
What is the collection structure?
Attributes
its the user id, and a role which can take the value "entrepreneur" or "coach"
Try to log this before sending
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
So I currently get this error:
"invalid document structure: Missing required attribute: userId"
Am i doing something wrong for the user id?
Can you share screenshot of the attribute screen?
This is how it looks
userid or userId?
userid
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
How would you suggest I fix this in order to store the attribute? Is there any part of the code I should correct?
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)
Are you referring to this, and saying make the capital I an i instead?
'userId': user.$id,
This is how will look your code after being modified:
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();
}
}
Yes
thank you! i'll give this a try. appreciate your help!
Confirm me if it works to mark this as solved π
It works! Thanks again!!
<a:agooglethumbsup:635256484682530825>
[SOLVED] Trying to store user preferences when creating an account
Recommended threads
- 1:1 relationship doesnβt sync after re-a...
Hi, Iβm trying to use a two-way one-to-one relationship. It works fine when I create a record with the relationship set, and it also works when I unset it. But ...
- Failed to create function
Hey everyone π I'm having an issue creating Functions on Appwrite Cloud and I'm not sure if it's a platform bug or something wrong in my project. When I try t...
- Upsert with setting permissions
Hi there, I am using self-hosted appwrite v1.7.4 and trying to use the bulk update stuff that was released with 1.7.x. Unfortunally I found that there is an ser...