我有 3 张桌子:
- 类别
- 子类别
- 类别子类别
1 个类别有 0 到多个子类别。
我有这个函数,它从类别表中获取所有条目,并且只获取属于 ID 为 1 的类别的子类别表的条目:
def getCategories(conn) do
categories = all Api.Category
groceryItemSubcategories = from s in Api.Subcategory,
join: cs in Api.CategorySubcategory, on: cs.c_id == 1,
select: %{name: s.name, foo: cs.c_id}
conn
|> put_resp_content_type("application/json")
|> send_resp(200, Poison.encode!(%{categories: categories, groceryItemSubcategories: groceryItemSubcategories}))
end
给出这个错误:
23:10:27.169 [error] #PID<0.339.0> running Api.Router terminated
Server: localhost:4000 (http)
Request: GET /categories
** (exit) an exception was raised:
** (Poison.EncodeError) unable to encode value: {"subcategories", Api.Subcategory}
(poison) lib/poison/encoder.ex:383: Poison.Encoder.Any.encode/2
(poison) lib/poison/encoder.ex:227: anonymous fn/4 in Poison.Encoder.Map.encode/3
(poison) lib/poison/encoder.ex:228: Poison.Encoder.Map."-encode/3-lists^foldl/2-0-"/3
(poison) lib/poison/encoder.ex:228: Poison.Encoder.Map.encode/3
(poison) lib/poison/encoder.ex:227: anonymous fn/4 in Poison.Encoder.Map.encode/3
(poison) lib/poison/encoder.ex:228: Poison.Encoder.Map."-encode/3-lists^foldl/2-0-"/3
(poison) lib/poison/encoder.ex:228: Poison.Encoder.Map.encode/3
(poison) lib/poison.ex:41: Poison.encode!/2
最后的答案是这样的:
def getCategories(conn) do
categories = all Api.Category
groceryItemSubcategories = Api.Repo.all(from s in Api.Subcategory,
join: cs in Api.CategorySubcategory, on: cs.s_id == s.id,
join: c in Api.Category, on: c.id == cs.c_id,
where: c.id == 1,
select: %{name: s.name, foo: cs.c_id}
)
conn
|> put_resp_content_type("application/json")
|> send_resp(200, Poison.encode!(%{categories: categories, groceryItemSubcategories: groceryItemSubcategories}))
end
使错误消失的部分是将语句包装在Api.Repo.All()
. Dogbert 确实是回答这个问题的人,所以我不想回答这个问题。