本文介绍了函数未定义或私有,但应该可以访问的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从我的帐户"域中找到一种方法.除了mark_as_admin/1(最后一个)之外,我可以使用iexAccounts域中的任何方法.

I'm trying to reach a method from my Accounts domain. I'm able to use any method from the Accounts domain in iex except for the mark_as_admin/1 (the last one).

这是域

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中输入的内容:

Here's what I type in 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: "[email protected]", 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: 
"[email protected]~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]})

为什么我不能调用此方法?

Why can't I call this method ?


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


After having restarted iex the methods in the domain can't be called anymore:

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

推荐答案

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

You should start IEx from your project's root directory with something like

iex -S mix.

如果您编辑代码并保存,则应该以相同的方式重启IEx,调用 recompile() ,或重新加载任何单个模块您已更改要使用的名称,例如:

If you edit your code and save it, you should either restart IEx the same way, call recompile(), or reload any single modules you've changed that you want to use, like:

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

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

这篇关于函数未定义或私有,但应该可以访问的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-15 16:20