freewebwithme
How can I serialize a schema(or list of schema) from database?
I am trying to send a cart information when a user join the channel. But having some trouble serializing a cart information.
in my cart_channel.ex
def join("cart:" <> user_id, _params, %{assigns: %{current_user: user}} = socket) do
# check if current user match client user
if String.to_integer(user_id) == user.id do
# If authorized, return cart information
{carts, total_items, total_price} = OrderManager.get_updated_carts(user.id)
IO.puts("Cart channel join successful")
socket =
assign(socket, :carts, carts)
|> assign(:total_items, total_items)
|> assign(:total_price, total_price)
send(self(), :send_cart)
{:ok, socket}
else
{:error, %{reason: "unauthenticated"}}
end
end
def join("cart:" <> _user_id, _params, _socket) do
{:error, %{reason: "unauthenticated"}}
end
def handle_info(
:send_cart,
socket = %{assigns: %{carts: carts, total_items: total_items, total_price: total_price}}
) do
push(socket, "cart", %{carts: carts, total_items: total_items, total_price: total_price})
{:noreply, socket}
end
and got an error like this
[error] GenServer #PID<0.1082.0> terminating
** (Protocol.UndefinedError) protocol Jason.Encoder not implemented for %MyApp.Checkout.Order{} of type MyApp.Checkout.Order (a struct), Jason.Encoder protocol must always be explicitly implemented.
How can I solve this?
Marked As Solved
Popular in Questions
I have a relationship of love and hate with Elixir. Lots of things are just absolutely right, but there are some things that are kind of ...
New
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New
Good day to you all.
I have been struggling to get a query involving like and ilike to work.
Can anyone assist me on this, please?
pro...
New
Hi all,
Trying to get some more clarity over utc_datetime and naive_datetime for Ecto:
https://hexdocs.pm/ecto/Ecto.Schema.html#module-...
New
I have followed this StackOverflow post to install the specific version of Erlang.
And When I am running mix ecto.setup then getting fol...
New
The Elixir Typespec docs show the following syntax for keyword lists in typespecs:
# ...
| [key: type] # keyword lis...
New
Original source of discussion: This topic on the Pragmatic Programmers' Functional Web Development with Elixir, OTP, and Phoenix forum.
...
New
I have a super simple question about elixir - how would I take a file like this
foo bar baz
and output a new file that enumerates th...
New
I am trying to run a deploy with docker and I successfully runned with this command:
docker build -t romenigld/blog-prod .
but when I t...
New
Hi everyone!
I need implement if…else if…else condition from my elixir code, and anymore of this control flow structures not work proper...
New
Other popular topics
Credo is smart enough to check for (something like) this:
assert length(the_list) == 0
with this response:
Checking if an enum is empt...
New
I have a server on AWS, and was running a load test using artillery. When looking at the Phoenix dashboard I see the Ports going to 100% ...
New
You’re a programmer, so you don’t need spoon feeding with the conventional drivel about “this is an integer.” No. You need to know what’s...
New
Hi folks,
Few months ago I have announced the proof-of-concept of the library to manipulate the browsers DOM objects directly from Elixi...
New
I’m working on defining a simple Ecto schema for a table (in PostGres), but I don’t see where I can define a column as NOT NULL. Conside...
New
This release brings a number of exciting features, including integration with the new Phoenix LiveDashboard and Phoenix LiveView. There h...
New
If I have a string “1000 cfu/ml” . I want to remove the characters and / and space . So the string is like this
"1000"
What is the ...
New
What’s the safe way to decode a JSON string into a struct? I want to avoid calling String.to_atom. Jason.decode can give me a map with st...
New
Hi,
I need to transform a list of numbers into a map where the keys are the indexes and the values are the original values of the list....
New
I’m not a pro in using Regex and can’t figure out why the following behaviour happens, especially if we take into account the difference ...
New







