Skip to content
Back

Help wanted in converting context.req.body to User model with Dart and appwrite function

  • 0
  • Users
  • Flutter
  • Functions
  • Cloud
safwan
30 Sep, 2023, 14:27

From the looks of it, this function creates a user-specific document. Why do you want to convert to a User object?

TL;DR
The user is trying to convert the `context.req.body` into a User model using Dart and an appwrite function. The user is using a map to access the values currently. There is a suggestion to check if `context.req.body` is a list and set it to an empty `Map<String, dynamic>` before using `User.fromMap()`. There is also a link to a GitHub issue related to this problem. The user mentions concerns about an empty list and not being able to check the type of the list. There is a suggestion to confirm the type of `user preferences` before converting it. It is mentioned that `body
james54
30 Sep, 2023, 14:28

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

james54
30 Sep, 2023, 14:29

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

safwan
30 Sep, 2023, 14:33

Okay but why do you want to use the User object in this function for that?

safwan
30 Sep, 2023, 14:34

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

safwan
30 Sep, 2023, 14:34

Just to be sure, what exactly do you want to convert into a User object?

james54
30 Sep, 2023, 14:46

I think i am not clear in what i am trying to say

james54
30 Sep, 2023, 14:47

I have implemented exactly what you said i am just storing IDs of users in friendIdList

james54
30 Sep, 2023, 14:49

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.

Drake
30 Sep, 2023, 15:47

You definitely shouldn't need to encode and decode like that

Drake
30 Sep, 2023, 15:48

How are you executing this function?

james54
30 Sep, 2023, 15:53

It is executed on user create event

Drake
30 Sep, 2023, 15:57

Right now, body is a string. In 1.4.4, it will be a map

james54
30 Sep, 2023, 15:58

So can i parse it?

Drake
30 Sep, 2023, 15:59

If you don't want your function to break you should jsonDecode(context.bodyRaw). That will give you a map

james54
30 Sep, 2023, 16:00

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

james54
30 Sep, 2023, 16:01

It will work if i directly assign it to a variable?

Drake
30 Sep, 2023, 16:01

I've seen an issue raised about user preferences...can you check the type of user preferences before converting it?

james54
30 Sep, 2023, 16:02

I think because the list is empty, dart can not check the type of List

Drake
30 Sep, 2023, 16:03

An empty list is still a type. However, it shouldn't be a list

Drake
30 Sep, 2023, 16:04

If you can confirm it's a list, we know where the problem is

james54
30 Sep, 2023, 16:08

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 😅 )

Drake
30 Sep, 2023, 16:21

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

Drake
30 Sep, 2023, 16:23

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

james54
30 Sep, 2023, 16:24

Thought so , For now i am using the map for accessing the values 👍

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