紧随《编程凤凰1.4版》 一书。当我使用手动数据库时,该模块运行良好,但是当我尝试切换accounts.ex
文件时(就像书中告诉我的那样),它坏了:
defmodule Rumbl.Accounts do
alias Rumbl.Repo
def get_user(id) do
Repo.get(User, id)
end
def get_user!(id) do
Repo.get!(User, id)
end
def get_user_by(params) do
Repo.get_by(User, params)
end
def list_users do
Repo.all(User)
end
end
这是我更改account.ex
文件时遇到的全部错误:
[error] #PID<0.732.0> running RumblWeb.Endpoint (connection #PID<0.731.0>, stream id 1) terminated
Server: localhost:4000 (http)
Request: GET /users
** (exit) an exception was raised:
** (Protocol.UndefinedError) protocol Ecto.Queryable not implemented for User, the given module does not exist. This protocol is implemented for: Atom, BitString, Ecto.Query, Ecto.SubQuery, Tuple
(ecto) lib/ecto/queryable.ex:40: Ecto.Queryable.Atom.to_query/1
(ecto) lib/ecto/repo/queryable.ex:14: Ecto.Repo.Queryable.all/3
(rumbl) lib/rumbl_web/controllers/user_controller.ex:7: RumblWeb.UserController.index/2
(rumbl) lib/rumbl_web/controllers/user_controller.ex:1: RumblWeb.UserController.action/2
(rumbl) lib/rumbl_web/controllers/user_controller.ex:1: RumblWeb.UserController.phoenix_controller_pipeline/2
(rumbl) lib/rumbl_web/endpoint.ex:1: RumblWeb.Endpoint.instrument/4
(phoenix) lib/phoenix/router.ex:275: Phoenix.Router.__call__/1
(rumbl) lib/rumbl_web/endpoint.ex:1: RumblWeb.Endpoint.plug_builder_call/2
(rumbl) lib/plug/debugger.ex:122: RumblWeb.Endpoint."call (overridable 3)"/2
(rumbl) lib/rumbl_web/endpoint.ex:1: RumblWeb.Endpoint.call/2
(phoenix) lib/phoenix/endpoint/cowboy2_handler.ex:34: Phoenix.Endpoint.Cowboy2Handler.init/2
答案 0 :(得分:2)
未为用户实现Ecto.Queryable,给定的模块不存在。
错误非常明显,这意味着您没有名为User
的模块(应用程序中可能是Rumbl.User
或Rumbl.Accounts.User
)。
假设您确实创建了ecto模式Rumbl.User
,则应该:
在您使用的模块中alias
之一:
alias Rumbl.User
或使用模块的标准名称:
Repo.get(Rumbl.User, id)