Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/elixir/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Elixir 长生不老药的地图改变了周围的结构_Elixir_Phoenix Framework - Fatal编程技术网

Elixir 长生不老药的地图改变了周围的结构

Elixir 长生不老药的地图改变了周围的结构,elixir,phoenix-framework,Elixir,Phoenix Framework,我的凤凰号密码缺少一些基本的东西 这是我的控制器 def show(conn, %{"id" => id}) do user = Repo.get!(User, id) query = from c in Card, where: c.user_id == 1, select: {c.id, c.inserted_at, c.estimate_name, c.product} estimates

我的凤凰号密码缺少一些基本的东西

这是我的控制器

  def show(conn, %{"id" => id}) do
    user = Repo.get!(User, id)

    query =
        from c in Card,
            where: c.user_id == 1,
            select: {c.id, c.inserted_at, c.estimate_name, c.product}
    estimates = Repo.all(query)

    render(conn, "show.json", %{user: user, estimates: estimates})
    # render(conn, "show.json", user: user)
  end
在我看来

  # def render("show.json", %{user: user}) do
  def render("show.json", %{user: user, estimates: estimates}) do
    %{data: render_one(%{user: user, estimates: estimates}, Api.UserView, "user.json") }
    # %{data: render_one(user, Api.UserView, "user.json")}

    #  ** New code since original post **
    # %{data: render("user.json", %{user: user, estimates: estimates})}
  end

  def render("user.json", %{user: user, estimates: estimates}) do
  # def render("user.json", %{user: user}) do
    %{id: user.id,
      firstname: user.firstname,
      lastname: user.lastname,
      email: user.email,
      customerId: user.customerId,
      estimates: render("estimates.json", estimates)}  # **Line with last error**
  end

  def render("estimates.json", [head | _estimates]) do
    #   Enum.map(estimates, fn estimate -> render(mapper estimate, MosaicApi.UserView, "summaryEstimate.json") } end)
    # render(mapper(head), MosaicApi.UserView, "summaryEstimate.json")
    render("summaryEstimate.json", mapper(head))

  end

  # ** I'm fear I have added unnecessary complexity here **
  def mapper({id, date, name, product}) do
      %{id: id,
        creation_date: date,
        estimate_name: name,
        product: product}
  end

  def render("summaryEstimate.json", estimate) do
    %{id: estimate.id,
      estimate_name: estimate.estimate_name,
      product: estimate.product}
  end
但是我得到了一个错误,这似乎是因为我的数据已经从
%{user:…,estimates:[…]}
转变为
%{user:%{estimates:[…],user:%{…}

这是怎么发生的,我怎样才能防止呢

Could not render "user.json" for MosaicApi.UserView, please define a matching clause for render/2 or define a template at "web/templates/user". No templates were compiled for this module.

Assigns:

%{user: %{estimates: [{1, #Ecto.DateTime<2016-04-26T12:01:34Z>, "cards for annabelle", "Business Cards"}, ...], user: %Api.User{__meta__: #Ecto.Schema.Metadata<:loaded>, customerId: "CUST", email: "foo@foo.be", firstname: "fname 1", id: 1, inserted_at: #Ecto.DateTime<2016-04-26T11:46:21Z>, jobs: #Ecto.Association.NotLoaded<association :jobs is not loaded>, lastname: "lname 1", updated_at: #Ecto.DateTime<2016-04-26T11:46:21Z>}}, view_module: Api.UserView, view_template: "user.json"}
无法为MosaicApi.UserView呈现“user.json”,请为render/2定义匹配子句或在“web/templates/user”定义模板。未为此模块编译模板。
分配:
%{用户:%{估计值:[{1,#eto.DateTime,“安娜贝勒的卡片”,“名片”},…],用户:%Api.user{uuuuu meta:#eto.Schema.Metadata,客户ID:“CUST”,电子邮件:foo@foo.be,姓氏:“fname 1”,id:1,插入地址:#exto.DateTime,作业:#exto.Association.notload,姓氏:“lname 1”,更新地址:#exto.DateTime},view_模块:Api.UserView,view_模板:“user.json”}
函数和从视图中改变赋值名称。我在

那么,当你打电话时会发生什么

render_one(%{user: user, estimates: estimates}, Api.UserView, "user.json")
使用以下参数调用
render

render("user.json", %{user: %{user: %{...}, estimates: [...]})
您可以使用
as
更改分配的名称:

render_one(%{user: user, estimates: estimates}, Api.UserView, "user.json", as: :data)
将使用以下命令调用渲染:

render("user.json", %{data: %{user: %{...}, estimates: [...]})
您可以将其与以下内容匹配:

def render("user.json", %{data: %{user: user, estimates: estimates}}) do

可以通过直接调用
render
而不是使用
render\u one
来避免这种情况:

render("user.json", %{user: user, estimates: estimates})
另一种选择是使用一种数据结构,将估计值嵌套在用户结构中。可能是通过向模式中添加虚拟属性

编辑

  def render("show.json", %{user: user, estimates: estimates}) do
    %{data: render("user.json", %{user: user, estimates: estimates})}
  end

  def render("user.json", %{user: user, estimates: estimates}) do
    %{id: user.id,
      ...
      estimates: render_many(estimates, __MODULE__, "estimates.json", estimates. as: estimate)}
  end

  def render("estimates.json", %{estimate: {id, _date, name, product}}) do
    %{id: id,
      estimate_name: name,
      product: product}
  end
这是怎么发生的,我怎样才能防止呢

Could not render "user.json" for MosaicApi.UserView, please define a matching clause for render/2 or define a template at "web/templates/user". No templates were compiled for this module.

Assigns:

%{user: %{estimates: [{1, #Ecto.DateTime<2016-04-26T12:01:34Z>, "cards for annabelle", "Business Cards"}, ...], user: %Api.User{__meta__: #Ecto.Schema.Metadata<:loaded>, customerId: "CUST", email: "foo@foo.be", firstname: "fname 1", id: 1, inserted_at: #Ecto.DateTime<2016-04-26T11:46:21Z>, jobs: #Ecto.Association.NotLoaded<association :jobs is not loaded>, lastname: "lname 1", updated_at: #Ecto.DateTime<2016-04-26T11:46:21Z>}}, view_module: Api.UserView, view_template: "user.json"}
这是因为
Phoenix.View.render_one
将您的分配包装在一个从视图名称屈折而来的键中

:

以下是:

render_one user, UserView, "show.html"
大致相当于:

if user != nil do
  render(UserView, "show.html", user: user)
end
基础用户作为
:user
传递给视图和模板,该用户由视图名称的变化而来

您应该直接调用
render/2

def render("show.json", %{user: user, estimates: estimates}) do
  %{data: render("user.json", %{user: user, estimates: estimates}}
end

这造成了很大的不同,但我现在得到了
参数错误(stdlib):maps.from_列表([{1,#Ec…
。我又添加了一些代码。我以为我的查询提供了一个列表,我可以对其进行模式匹配,但不确定这是否是事实。看起来你有点偏离了轨道,请检查我的编辑。谢谢!!看起来你有一个打字错误,因为它最终需要
估计:呈现多个(估计,u模块uuuuuuuuuuu),“estimates.json”,as::estimate)}
这带来了很大的不同,但我现在得到了
参数错误(stdlib):maps.from_list([{1,#Ec…
),我添加了更多的代码。我认为我的查询提供了一个列表,我可以对其进行模式匹配,但不确定事实上是这样的