【问题标题】:no function clause matching in Access.get/3 - Why am I getting this error?Access.get/3 中没有函数子句匹配 - 为什么会出现此错误?
【发布时间】:2019-11-03 20:07:19
【问题描述】:

当我尝试从数据库中获取列表时出现此错误,我不明白为什么。

这是引发错误的函数:

  def list_lectures do
    Lecture
    |> Repo.all()
    |> Repo.preload(:author [user: :credential])
  end

这是我运行代码时遇到的错误:

iex(1)> Ram.CMS.list_lectures
[debug] QUERY OK source="lectures" db=3.4ms decode=0.7ms queue=0.4ms
SELECT l0."id", l0."source", l0."theme", l0."time", l0."title", l0."author_id", l0."inserted_at", l0."updated_at" FROM "lectures" AS l0 [] 
** (FunctionClauseError) no function clause matching in Access.get/3    

    The following arguments were given to Access.get/3:

        # 1
        :author

        # 2
        [user: :credential]

        # 3
        nil

    Attempted function clauses (showing 5 out of 5):

        def get(%module{} = container, key, default)
        def get(map, key, default) when is_map(map)
        def get(list, key, default) when is_list(list) and is_atom(key)
        def get(list, key, _default) when is_list(list)
        def get(nil, _key, default)

    (elixir) lib/access.ex:265: Access.get/3
    (ram) lib/ram/cms.ex:24: Ram.CMS.list_lectures/0
iex(1)> 
  schema "lectures" do
    field :source, :string
    field :theme, :string
    field :time, :string
    field :title, :string
    belongs_to :author, Author

    timestamps()
  end
  schema "credentials" do
    field :email, :string
    field :password, :string
    belongs_to :user, User

    timestamps()
  end
schema "users" do
    field :name, :string
    field :role, :string
    has_one :credential, Credential

    timestamps()
  end

这是项目链接:https://github.com/DavidNeumark/ram

【问题讨论】:

  • 我认为预加载应该是[:author, [user: :credential]]。您缺少分号。
  • 谢谢,TheAnh,我按照你说的做了,但还是有问题。

标签: elixir phoenix-framework


【解决方案1】:

Access.get/3 文件说:

获取容器中给定键的值(映射、关键字列表或实现访问行为的结构)。

你的第一个参数给 Access.get/3:

:author 不是映射、关键字列表或结构。是原子。

我猜你想预加载嵌套关联,所以它应该是这样的:

  def list_lectures do
    Lecture
    |> Repo.all()
    |> Repo.preload(author: [user: :credential])
  end

更新:检查你的 github 后,lib/ram/cms/lecture.ex 错过了 Author 的别名。它导致上面的代码不起作用

defmodule Ram.CMS.Lecture do
  use Ecto.Schema
  import Ecto.Changeset

  alias Ram.CMS.Author

  schema "lectures" do
    field :source, :string
    field :theme, :string
    field :time, :string
    field :title, :string
    belongs_to :author, Author

    timestamps()
  end

  @doc false
  def changeset(lecture, attrs) do
    lecture
    |> cast(attrs, [:title, :time, :source, :theme])
    |> validate_required([:title, :time, :source, :theme])
  end
end

【讨论】:

  • 我修改了答案,看看
  • 是的,它确实缺少“别名”,我已经在 Github 上更新了项目......但我仍然有错误。我试图在 iex 上运行 Ram.Repo.all () 并且也抛出了一个错误。也许您对如何找到解决方案有任何想法?
  • 那是什么错误?正如我所检查的,在 Repo.preload(author: [user: :credential]) 修改 list_lectures 函数并在 Lecture 架构上添加错过的 Author 别名 后,上述问题已得到解决
  • @Minh-Khang 你能帮帮我吗:stackoverflow.com/questions/65183075/…
猜你喜欢
  • 1970-01-01
  • 2021-02-10
  • 1970-01-01
  • 2017-11-26
  • 2023-03-13
  • 2013-05-21
  • 2019-10-28
  • 1970-01-01
  • 2021-04-03
相关资源
最近更新 更多