【问题标题】:Route to controller which fetches from database and returns results in http response路由到从数据库中获取并以 http 响应返回结果的控制器
【发布时间】:2017-04-15 20:59:34
【问题描述】:

我想要一个从数据库表中获取所有条目并返回它们的路由。

在 router.ex 中:

  get "/categories/" do
    [controller] = ["repo"]
    Api.Repo.getCategories(conn, controller)
  end

在 repo.ex 中:

  def getCategories(conn, controller) do
    conn
    start_link
    categories = Api.Category |> all
    |> put_resp_content_type("application/json")
    |> send_resp(200, categories)
  end

Api.Category.ex

defmodule Api.Category do
  use Ecto.Schema

  schema "categories" do
    field :name, :string
  end

  def changeset(category, params \\ %{}) do
    category
    |> Ecto.Changeset.cast(params, [:name])
    |> Ecto.Changeset.validate_required([:name])
  end
end

我收到以下警告和错误:

warning: variable conn in code block has no effect as it is never returned (remove the variable or a
ssign it to _ to avoid warnings)
  lib/api/repo.ex:174

warning: variable "start_link" does not exist and is being expanded to "start_link()", please use pa
rentheses to remove the ambiguity or change the variable name
  lib/api/repo.ex:175

warning: variable "categories" does not exist and is being expanded to "categories()", please use pa
rentheses to remove the ambiguity or change the variable name
  lib/api/repo.ex:178

warning: variable "controller" is unused
  lib/api/repo.ex:173

warning: variable "categories" is unused
  lib/api/repo.ex:176


== Compilation error on file lib/api/repo.ex ==
** (CompileError) lib/api/repo.ex:178: undefined function categories/0
    (stdlib) lists.erl:1338: :lists.foreach/2
    (stdlib) erl_eval.erl:670: :erl_eval.do_apply/6

看起来我正在使用categories。我做错了什么?

【问题讨论】:

  • 哦,我的......这是一些很好的代码质量。

标签: controller routes elixir ecto postgrex


【解决方案1】:

你的错误是由

引起的
categories = Api.Category |> all
|> put_resp_content_type("application/json")
|> send_resp(200, categories)

这是分配给类别的单个管道。因此,send_resp(200, categories) 中的类别未设置。

为了清楚起见,这是编写相同代码的另一种方式:

categories = 
  Api.Category 
  |> all
  |> put_resp_content_type("application/json")
  |> send_resp(200, categories)

此外,connstart_link 放错了位置。 conn 什么都不做,start_link 通常会返回一个丢失的 pid。

我相信你正在尝试类似的东西:

  categories = all Api.Category
  conn
  |> put_resp_content_type("application/json")
  |> send_resp(200, categories)

整个例子看起来很奇怪。我从来没有在 Repo 模块中看到过这种类型的东西。通常,您会在控制器中找到它。我无法评论 route.ex 文件,因为我以前从未创建过这样的路线。

你在学习一个例子吗?如果有,是什么?

【讨论】:

  • 谢谢!我已经完成了this Ecto getting started pagethis tutorial about getting started with Elixir plug routes,我正在尝试将它们混合在一起以创建一个 API,该 API 允许前端与数据库接口,检索并插入数据库条目。我确实想要一个控制器,但有点迷失了。我将尝试将数据库查询移动到控制器。我想既然它作用于数据库 Repo,它应该在 Repo 模块中。
  • 我试图做的是选择“类别”数据库表中的所有条目并将它们返回到 http 响应正文中。您的示例是否获取类别数据库表中的条目?
  • 我收到repo Api.Repo is not started, please ensure it is part of your supervision tree 错误
  • start_link 是我开始回购的尝试。我正在通过 Jose Valims 评论“存储库未启动。在查询之前尝试 Weather.Repo.start_link。” here
  • 是的,我的示例加载了数据库中的所有类别。 repo 只需要在启动时启动一次。因此,您不会在可以一遍又一遍地调用的 get 函数中这样做。您应该在主管中启动回购。您可能希望创建一个 phoenix 项目作为示例,因为它会在正确的位置创建所有内容。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-11-21
  • 1970-01-01
  • 2017-11-09
  • 2018-12-03
相关资源
最近更新 更多