0

我有 3 张桌子:

  1. 类别
  2. 子类别
  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 确实是回答这个问题的人,所以我不想回答这个问题。

4

1 回答 1

1

原始代码有两个主要问题:

  1. 您忘记调用Repo.all第二个查询。

  2. 您正在查询中选择一个元组,然后将其编码为 JSON。Poison 确实将元组编码为 JSON。您可以选择列表或地图,具体取决于您想要的数据结构。选择地图的方法如下:

    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, c_id: cs.c_id})
    
于 2017-04-22T13:14:35.663 回答