shahryarjb
How to convert multi struct in a list to Json in phoenix controller
Hello, I have a struct that was made with preload, this output cannot be converted to json encode.
it should be noted, I can’t use @derive in my schema files, but I use it in my query file, but the task was broken , because I have multi struct in a list.
now how can I convert it to json, I’m using Jason lib,
my preload:
def show_support_with_code(code, user_id) do
query = from u in SupportSchema,
where: u.code == ^code,
join: a in assoc(u, :users),
# where: a.id == ^user_id,
left_join: b in assoc(u, :blog_posts),
left_join: c in assoc(u, :support_replies),
left_join: d in assoc(c, :users),
order_by: [desc: c.inserted_at],
preload: [users: a, blog_posts: b, support_replies: {c, users: d}]
case Repo.one(query) do
nil -> {:error, :show_support_with_code}
info -> {:ok, :show_support_with_code, info}
end
end
output preload:
%BankError.ClientApi.Support.SupportSchema{
__meta__: #Ecto.Schema.Metadata<:loaded, "supports">,
blog_posts: nil,
code: "test47",
description: "بررسی بهینه سازی در اگهی",
id: "cd33f63f-44c5-46a7-848c-8926be21dd82",
inserted_at: ~N[2019-08-25 17:43:49],
post_id: nil,
status: 3,
support_replies: [
%BankError.Support.SupportReplySchema{
__meta__: #Ecto.Schema.Metadata<:loaded, "support_replies">,
description: "برای تست اینجاییم",
id: "e67dae2f-237c-439f-bee7-846f8f575cc9",
inserted_at: ~N[2019-08-29 17:24:25],
status: true,
support_id: "cd33f63f-44c5-46a7-848c-8926be21dd82",
supports: #Ecto.Association.NotLoaded<association :supports is not loaded>,
updated_at: ~N[2019-08-29 17:24:26],
user_id: "40c06a6b-15a0-4888-999b-132fbd326455",
users: %BankError.Users.UserSchema{
__meta__: #Ecto.Schema.Metadata<:loaded, "users">,
id: "40c06a6b-15a0-4888-999b-132fbd326455",
inserted_at: ~N[2019-05-26 08:12:14],
last_name: "soam",
mobile: "09213780329",
name: "shiam",
password: nil,
password_hash: "$2b$12$N2c6fKCVez7ZB/5k7coXkuwNaVGESQboFNba8cmRcRqoaKufs4PAC",
role: 1,
status: 1,
support_replies: #Ecto.Association.NotLoaded<association :support_replies is not loaded>,
supports: #Ecto.Association.NotLoaded<association :supports is not loaded>,
transactions: #Ecto.Association.NotLoaded<association :transactions is not loaded>,
updated_at: ~N[2019-05-26 08:12:14]
}
}
],
title: "ساختمان سازی یک تست دوم",
type: "other",
updated_at: ~N[2019-08-26 06:29:23],
user_id: "d0c1c8c5-6747-47c7-ba21-89a15017be3c",
users: %BankError.Users.UserSchema{
__meta__: #Ecto.Schema.Metadata<:loaded, "users">,
id: "d0c1c8c5-6747-47c7-ba21-89a15017be3c",
inserted_at: ~N[2019-04-16 08:20:10],
last_name: "tavakkoli",
mobile: "09368094936",
name: "shahryar",
password: nil,
password_hash: "$2b$12$k/Ql7o9VyUbeeHi3Dr1BuOub2Wdg6NZ0I8bIfM8SmaqNop8o60H.G",
role: 3,
status: 2,
support_replies: #Ecto.Association.NotLoaded<association :support_replies is not loaded>,
supports: #Ecto.Association.NotLoaded<association :supports is not loaded>,
transactions: #Ecto.Association.NotLoaded<association :transactions is not loaded>,
updated_at: ~N[2019-08-26 05:38:18]
}
}
Marked As Solved
shahryarjb
Hello again, I made some progress
I added this line in my controller (BankErrorWeb.ApiSupportController):
require Protocol
Protocol.derive(Jason.Encoder, BankError.ClientApi.Support.SupportSchema, only: [:title, :description, :type, :status, :code, :users, :blog_posts])
after adding top line code in my controller I added @derive {Jason.Encoder, only: [..]} into my schema files that were connected like, (users, blog_posts, support_replies)
it works but it has a problem, because I don’t input support_replies: {c, users: d} in my preload, I mean
Protocol.derive(Jason.Encoder, BankError.ClientApi.Support.SupportSchema, only: [:title, :description, :type, :status, :code, :users, :blog_posts, :support_replies])
if support_replies added !!!
it shows me this error:
** (exit) an exception was raised:
** (Protocol.UndefinedError) protocol Jason.Encoder not implemented for %BankError.Support.SupportReplySchema{__meta__: #Ecto.Schema.Metadata<:loaded, "support_replies">
def show_support_with_code(code, user_id) do
query = from u in SupportSchema,
where: u.code == ^code,
join: a in assoc(u, :users),
where: a.id == ^user_id,
left_join: b in assoc(u, :blog_posts),
left_join: c in assoc(u, :support_replies),
left_join: d in assoc(c, :users),
order_by: [desc: c.inserted_at],
preload: [users: a, blog_posts: b, support_replies: {c, users: d}]
case Repo.one(query) do
nil -> {:error, :show_support_with_code}
info -> {:ok, :show_support_with_code, info}
end
end
update
it is fixed, I had a bad schema relation
Thanks
Also Liked
albydarned
Have you tried a simple
Map.from_struct
shahryarjb
I tested it
def test1() do
[
%BankError.Support.SupportReplySchema{
__meta__: "s",
description: "برای تست اینجاییم",
id: "e67dae2f-237c-439f-bee7-846f8f575cc9",
inserted_at: ~N[2019-08-29 17:24:25],
status: true,
support_id: "cd33f63f-44c5-46a7-848c-8926be21dd82",
supports: "#Ecto.Association.NotLoaded<association :supports is not loaded>",
updated_at: ~N[2019-08-29 17:24:26],
user_id: "40c06a6b-15a0-4888-999b-132fbd326455",
users: %BankError.Users.UserSchema{
__meta__: "",
id: "40c06a6b-15a0-4888-999b-132fbd326455",
inserted_at: ~N[2019-05-26 08:12:14],
last_name: "soam",
mobile: "09213780329",
name: "shiam",
password: nil,
password_hash: "$2b$12$N2c6fKCVez7ZB/5k7coXkuwNaVGESQboFNba8cmRcRqoaKufs4PAC",
role: 1,
status: 1,
support_replies: "#Ecto.Association.NotLoaded<association :support_replies is not loaded>",
supports: "#Ecto.Association.NotLoaded<association :supports is not loaded>",
transactions: "#Ecto.Association.NotLoaded<association :transactions is not loaded>",
updated_at: ~N[2019-05-26 08:12:14]
}
}
]
|> test
end
def test(struct) do
struct
|> Enum.map(fn %{users: users} = x ->
x
|> Map.delete(:__struct__) # change the struct to a Map
|> Map.drop([:__meta__])
|> Enum.filter(fn {c, v} -> v end)
|> Enum.into(%{})
end)
end
but the output is:
I can’t be able to delete %BankError.Users.UserSchema and some field like __meta__ and etc in users map
help me please
albydarned
I can’t use @derive in my schema files
Why?
Popular in Questions
Other popular topics
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #channels
- #elixirconf
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #phoenix_html
- #iex
- #blog-post
- #graphql
- #genstage
- #ai
- #websockets
- #supervisor
- #advent-of-code
- #elixirconf-us
- #distillery
- #processes
- #forms
- #api
- #metaprogramming
- #security
- #performance










