我想要一个从数据库表中获取所有条目并返回它们的路由。
在 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
. 我究竟做错了什么?