函数是未定义的或私有的,但应该是可访问的

时间:2018-01-14 12:18:21

标签: elixir phoenix-framework

我试图从我的帐户域中获取方法。我可以使用Accountsiex域中的任何方法,mark_as_admin/1(最后一个)除外。

这是域名

defmodule Storex.Accounts do
  import Ecto.Query, warn: false
  alias Storex.Repo
  alias Storex.Accounts.User

  def create_user(attrs \\ %{}) do
    %User{}
    |> User.changeset(attrs)
    |> Repo.insert()
  end

  def get_user!(id) do
    Repo.get!(User, id)
  end

  def new_user() do
    User.changeset(%User{}, %{})
  end

  def authenticate_user(email, password) do
    Repo.get_by(User, email: email)
    |> User.check_password(password)
  end

  def mark_as_admin(user) do
    user
    |> User.admin_changeset(%{is_admin: true})
    |> Repo.update()
  end
end

这是我在iex中输入的内容:

iex(1)> alias Storex.Accounts
iex(2)> user = Accounts.get_user!(7)
[debug] QUERY OK source="accounts_users" db=2.2ms
SELECT a0."id", a0."email", a0."full_name", a0."password_hash", 
a0."is_admin", a0."inserted_at", a0."updated_at" FROM "accounts_users" 
AS a0 WHERE (a0."id" = $1) [7]

%Storex.Accounts.User{__meta__: #Ecto.Schema.Metadata<:loaded, 
"accounts_users">,

email: "test@user.tld", full_name: "Test User", id: 7,

inserted_at: ~N[2018-01-14 11:52:14.472928], is_admin: false, password: nil,

password_hash: 
"$2b$12$I7Kq8SgkWN77W3jx2QwaNe.so6z75xtYOIzl5Ws5kqlaTniLXMyIe",

updated_at: ~N[2018-01-14 11:52:14.472935]}

iex(3)> Accounts.mark_as_admin(user)
** (UndefinedFunctionError) function Storex.Accounts.mark_as_admin/1 is             
undefined or private
(storex) Storex.Accounts.mark_as_admin(%Storex.Accounts.User{__meta__:     
#Ecto.Schema.Metadata<:loaded, "accounts_users">, email: 
"test@user.t~ld",   full_name: "Test User", id: 7, inserted_at: 
~N[2018-01-14 11:52:14.472928], is_admin: false, password: nil, 
password_hash: 
"$2b$12$I7Kq8SgkWN77W3jx2QwaNe.so6z75xtYOIzl5Ws5kqlaTniLXMyIe", 
updated_at: ~N[2018-01-14 11:52:14.472935]})

为什么我不能称这种方法?

编辑:
重新启动iex后,域名中的方法将无法再被调用:

iex(1)> alias Storex.Accounts  
Storex.Accounts  
iex(2)> Accounts.get_user!(7)  
** (UndefinedFunctionError) function Storex.Accounts.get_user!/1 is   
undefined or private  

1 个答案:

答案 0 :(得分:2)

您应该从项目的根目录启动IEx,例如

iex -S mix

如果您编辑并保存代码,则应以相同方式重新启动IEx,致电recompile()reload any single modules您已更改了要使用的内容,例如:

iex> recompile() # recompiles and reloads everything except mix.exs and configs

iex> r Storex.Accounts # recompiles and reloads a single module