协议Ecto.Queryable未被实现

13

我正在尝试将Guardian集成到我的API中,并通过它进行登录以获取JWT。我正在查看的教程在这里。问题是要使用类似于示例中使用的User Model来实现登录。Model代码看起来像:

defmodule PushflightServer.User do
  use PushflightServer.Web, :model

use Ecto.Repo
import Ecto.Query
  alias PushflightServer.Repo

  schema "users" do
    field :name, :string
    field :email, :string
    field :encrypted_password, :string
    field :password, :string, virtual: true
    field :verify_token, :string
    field :verify_date, Ecto.DateTime

    timestamps
  end

  def from_email(nil), do: { :error, :not_found }
  def from_email(email) do
    IO.write("Before email")
    IO.inspect(email)
    Repo.one(User, email: email)
  end
如果我在Phoenix中或者在中调用from_email,会出现以下错误信息:

** (Protocol.UndefinedError) protocol Ecto.Queryable not implemented for User, the given module does not exist (ecto) lib/ecto/queryable.ex:33: Ecto.Queryable.Atom.to_query/1 (ecto) lib/ecto/repo/queryable.ex:90: Ecto.Repo.Queryable.execute/5 (ecto) lib/ecto/repo/queryable.ex:15: Ecto.Repo.Queryable.all/4 (ecto) lib/ecto/repo/queryable.ex:44: Ecto.Repo.Queryable.one/4

我一定是漏掉了某个简单的东西,但我一直没有找到任何关于为什么会出现这种情况的资料。使用Repo插入数据时一切正常。有任何想法吗?
2个回答

9

您需要将User完全命名为PushflightServer.User,或者您可以使用快捷方式__MODULE__


3

你应该使用命名空间来引用模块

  def from_email(email) do
    PushflightServer.one(PushflightServer.User, email: email)
  end

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接