Error no case clause matching

Hey,

I’m stuck because of a no case clause matching error can you help please :slight_smile:

# #Api.Entity.User<__meta__: #Ecto.Schema.Metadata<:loaded, "users">, clocks: #Ecto.Association.NotLoaded<association :clocks is not loaded>, confirmed_at: ~U[2021-11-15 02:53:34.000000Z], email: "jean.jean@jean.com", firstname: nil, id: "3fe428fc-98fa-488d-8da4-f8471babef0f", inserted_at: ~N[2021-11-15 02:53:33], lastname: nil, phonenumber: nil, role_id: "09f4a202-0b5a-4ee1-9b68-5384ac1c551a", roles: %Api.Entity.Role{__meta__: #Ecto.Schema.Metadata<:loaded, "roles">, id: "01f4a202-0b5a-4ee1-9b68-5384ac1c551a", inserted_at: ~N[2021-11-14 00:00:00], name: "Employee", updated_at: ~N[2021-11-14 00:00:00], users: #Ecto.Association.NotLoaded<association :users is not loaded>}, team_id: "09f4a202-0b5a-4ee1-9b68-5384ac1c552a", team_manager: false, teams: %Api.Entity.Team{__meta__: #Ecto.Schema.Metadata<:loaded, "teams">, id: "09f4a202-0b5a-4ee1-9b68-5384ac1c552a", inserted_at: ~N[2021-11-14 00:00:00], name: "General", updated_at: ~N[2021-11-14 00:00:00], users: #Ecto.Association.NotLoaded<association :users is not loaded>, workingtimes: #Ecto.Association.NotLoaded<association :workingtimes is not loaded>}, updated_at: ~N[2021-11-15 02:53:33], username: "Amaury2", workingtimes: #Ecto.Association.NotLoaded<association :workingtimes is not loaded>, ...>

  def login(connection, %{"email" => email, "password" => password}) do
    case Api.Entity.get_user_by_email_and_password(email, password) do
    {:ok, user} ->
        token = Api.Entity.generatetoken(user)
        connection
        |> put_status(:accepted)
        |> json(%{
          user: %{
            id: user.id,
            email: user.email,
            username: user.username,
            firstname: user.firstname,
            lastname: user.lastname,
            phonenumber: user.phonenumber,
          },
          token: token
        })
      {:error, _ } ->
        connection
        |> put_status(400)
        |> json(%{error: "Invalid creadentials"})
    end
  end

    field :email, :string
    field :username, :string
    field :password, :string, virtual: true, redact: true
    field :hashed_password, :string, redact: true
    field :firstname, :string
    field :lastname, :string
    field :phonenumber, :string
    field :team_manager, :boolean, default: false
    belongs_to :teams, Api.Entity.Team, foreign_key: :team_id, type: :binary_id
    belongs_to :roles, Api.Entity.Role, foreign_key: :role_id, type: :binary_id
    has_many :clocks, Api.Entity.Clock
    has_many :workingtimes, Api.Entity.WorkingTime
    field :confirmed_at, :utc_datetime_usec

  def get_user_by_email_and_password(email, password) do
    user = Api.Repo.get_by(Api.Entity.User, email: email) |> Api.Repo.preload([:roles, :teams]);
    if Api.Entity.User.valid_password?(user, password), do: user
  end

The error seems to come from get_user_by_email_and_password I think or maybe because of not everything preloaded I don’t know and i’m stuck, it s why i come here, thanks a lot ^^

Can you share what this above function is returning? But a debugger and assign the value to some temp variable and then check what is the above returning ?

My bad it is returning user. Now can you change that to {:ok, user}

You should not just remove your post when it’s solved. It may help someone else.

3 Likes

You are right…

I did undo the change, for future reference.

This code is expecting get_user_by_email_and_password to return either a tuple starting with :ok or a tuple starting with :error

This code either returns a User or nil (if the if’s condition is false). This is not what the case statement expects.

2 Likes