From the looks of it, this function creates a user-specific document. Why do you want to convert to a User object?
Help wanted in converting context.req.body to User model with Dart and appwrite function
Votes
0
Replies
24
Participants
Unknown
Messages
25
Rank 2: Process
One other thing i also need to encode-decode "context.req.body" for using it, i don't think i am doing it right also
Rank 2: Process
I have created a friend request type system in my app. so i need to show other users to app user so i thought it will be a better way
Okay but why do you want to use the User object in this function for that?
A better approach would be to store just the user IDs in the friendIdList attribute, and use the IDs to fetch and display each user
Just to be sure, what exactly do you want to convert into a User object?
Rank 2: Process
I think i am not clear in what i am trying to say
Rank 2: Process
I have implemented exactly what you said i am just storing IDs of users in friendIdList
Rank 2: Process
I am trying to say that ,
How can i convert context.req.body into a User model so that i can access properties (name,email) like userObject.name , userObject.email. & not have to userMap['key'] to use its value
I hope you are getting it.
Admin
You definitely shouldn't need to encode and decode like that
Admin
How are you executing this function?
Rank 2: Process
It is executed on user create event
Admin
Right now, body is a string. In 1.4.4, it will be a map
Rank 2: Process
So can i parse it?
Admin
If you don't want your function to break you should jsonDecode(context.bodyRaw). That will give you a map
Rank 2: Process
I think i am getting the above mentioned error when parsing map to User model but i didn't try with bodyRaw , let me check
Rank 2: Process
It will work if i directly assign it to a variable?
Admin
I've seen an issue raised about user preferences...can you check the type of user preferences before converting it?
Rank 2: Process
I think because the list is empty, dart can not check the type of List
Admin
An empty list is still a type. However, it shouldn't be a list
Admin
If you can confirm it's a list, we know where the problem is
Rank 2: Process
I have not touched preferences ,I don't know how it is set to an empty list (First i thought it is supposed to be a list but now that you said,I checked again 😅 )
Admin
Ya it's a bug. You might want to 👍 this issue and comment saying it's a problem in functions too: https://github.com/appwrite/appwrite/issues/6360
Admin
What you can do now is after JSON decode, check if it's a list and if it is, set it to an empty Map<String, dynamic>. Then, doing User.fromMap() should work
Rank 2: Process
Thought so , For now i am using the map for accessing the values 👍