Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

protocol Ecto.Queryable not implemented for

I'm trying to implement Guardian into my API and perform a login through it to get JWT back. The tutorial I'm looking at is here. The problem is implementing the login using a User Model similar to what they're using in the example. The Model code looks like:

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

If I call the from_email either from within Phoenix or straight in iex -S mix, I get the following error:

user = PushflightServer.User.from_email("[email protected]")

** (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

I must be missing something simple, but I haven't been able to find any documentation about why this is happening. The insert of the data using the Repo worked fine. Any ideas?

like image 590
Rob Avatar asked Mar 03 '16 04:03

Rob


2 Answers

You need to fully namespace User as PushflightServer.User or you can use the shortcut __MODULE__

like image 128
Robo Avatar answered Nov 05 '22 01:11

Robo


you should reference the modules with namespaces

  def from_email(email) do
    PushflightServer.one(PushflightServer.User, email: email)
  end
like image 22
allyraza Avatar answered Nov 05 '22 00:11

allyraza