Elixir 预加载的数据在转换为JSON时丢失

Elixir 预加载的数据在转换为JSON时丢失,elixir,phoenix-framework,ecto,Elixir,Phoenix Framework,Ecto,在我的上下文中,我有以下预加载所有者和受让人,它们都属于用户 def list_tasks do Repo.all(Task) |> Repo.preload([:owner, :assignee]) end 在我的索引控制器中,我有如下内容: def index(conn, _params) do tasks = Issue.list_tasks() IO.inspect(tasks) render(conn, "index.json",

在我的上下文中,我有以下预加载所有者和受让人,它们都属于用户

  def list_tasks do
    Repo.all(Task)
    |> Repo.preload([:owner, :assignee])
  end
在我的索引控制器中,我有如下内容:

def index(conn, _params) do
    tasks = Issue.list_tasks()
    IO.inspect(tasks)
    render(conn, "index.json", tasks: tasks)
end
defmodule MyAppWeb.TaskView do
    use MyAppWeb, :view
    alias MyAppWeb.TaskView

    def render("index.json", %{tasks: tasks}) do
        %{data: render_many(tasks, TaskView, "task.json")}
    end

    def render("show.json", %{task: task}) do
        %{data: render_one(task, TaskView, "task.json")}
    end

    def render("task.json", %{task: task}) do
        %{
            id: task.id,
            title: task.title,
            timespent: task.timespent,
            details: task.details,
            status: task.status
        }
    end
end
defmodule MyAppWeb.TaskView do
    use MyAppWeb, :view
    alias MyAppWeb.{TaskView, AsigneeView}

    ...

    def render("task.json", %{task: task} do
        %{
            id: task.id,
            ...
            asignee: render_one(task.asignee, AsigneeView, "asignee.json"),
        }
    end
end
IO.inspect(任务)打印出来

[
  %Task3.Issue.Task{
    __meta__: #Ecto.Schema.Metadata<:loaded, "tasks">,
    assignee: %Task3.Accounts.User{
      __meta__: #Ecto.Schema.Metadata<:loaded, "users">,
      email: "Jill@Jill.com",
      id: 2,
      inserted_at: ~N[2018-04-02 18:22:21.699478],
      updated_at: ~N[2018-04-02 18:22:21.699486],
      username: "Jill"
    },
    assignee_id: 2,
    details: nil,
    id: 1,
    inserted_at: ~N[2018-04-02 18:22:21.711588],
    owner: %Task3.Accounts.User{
      __meta__: #Ecto.Schema.Metadata<:loaded, "users">,
      email: "Jack@Jack.com",
      id: 1,
      inserted_at: ~N[2018-04-02 18:22:21.677877],
      updated_at: ~N[2018-04-02 18:22:21.677887],
      username: "Jack"
    },
    owner_id: 1,
    status: "COMPLETE",
    timespent: nil,
    title: "test",
    updated_at: ~N[2018-04-02 18:22:21.711598]
  }
]

我失去了受让人和所有者。我遗漏了什么吗?要将预加载的数据转换成json表单,是否需要采取额外的步骤?

您需要检查
任务\u view.ex
文件

如果它是使用
phx.gen.json
生成的默认文件,它将如下所示:

def index(conn, _params) do
    tasks = Issue.list_tasks()
    IO.inspect(tasks)
    render(conn, "index.json", tasks: tasks)
end
defmodule MyAppWeb.TaskView do
    use MyAppWeb, :view
    alias MyAppWeb.TaskView

    def render("index.json", %{tasks: tasks}) do
        %{data: render_many(tasks, TaskView, "task.json")}
    end

    def render("show.json", %{task: task}) do
        %{data: render_one(task, TaskView, "task.json")}
    end

    def render("task.json", %{task: task}) do
        %{
            id: task.id,
            title: task.title,
            timespent: task.timespent,
            details: task.details,
            status: task.status
        }
    end
end
defmodule MyAppWeb.TaskView do
    use MyAppWeb, :view
    alias MyAppWeb.{TaskView, AsigneeView}

    ...

    def render("task.json", %{task: task} do
        %{
            id: task.id,
            ...
            asignee: render_one(task.asignee, AsigneeView, "asignee.json"),
        }
    end
end
您需要编辑此文件并添加要显示的额外字段

e、 g.对于
受让人
,您可以重复使用自动生成的视图,如下所示:

def index(conn, _params) do
    tasks = Issue.list_tasks()
    IO.inspect(tasks)
    render(conn, "index.json", tasks: tasks)
end
defmodule MyAppWeb.TaskView do
    use MyAppWeb, :view
    alias MyAppWeb.TaskView

    def render("index.json", %{tasks: tasks}) do
        %{data: render_many(tasks, TaskView, "task.json")}
    end

    def render("show.json", %{task: task}) do
        %{data: render_one(task, TaskView, "task.json")}
    end

    def render("task.json", %{task: task}) do
        %{
            id: task.id,
            title: task.title,
            timespent: task.timespent,
            details: task.details,
            status: task.status
        }
    end
end
defmodule MyAppWeb.TaskView do
    use MyAppWeb, :view
    alias MyAppWeb.{TaskView, AsigneeView}

    ...

    def render("task.json", %{task: task} do
        %{
            id: task.id,
            ...
            asignee: render_one(task.asignee, AsigneeView, "asignee.json"),
        }
    end
end

我想你会想看看你对那个控制器的看法。默认情况下,由于某些字段(如
\uuuuuuu meta\uuuuu
)的原因,我们无法将结构转换为JSON。因此,我们只需要取出我们想要的字段。在您看来,您很可能有一个渲染函数来实现这一点。谢谢!我忘了换个角度!