maz

maz

Elixir Function Succeeding but not Returning Value

Hi all, I’m not clear why this function is not returning a value:


  def confirm_email!(user = %User{email_confirmed: false}) do
    confirm = User.changeset_confirm_email(user, true)
    case Repo.update(confirm) do
      # Updated with success
      {:ok, updated_user} ->
        Logger.debug("update success")
        {:ok, updated_user}
      {:error, _, reason, _} ->
        Logger.debug("update error")
        Logger.error(reason)
        {:error, reason}
    end
    Logger.debug("end of confirm_email")
  end

I see in the log:

[debug] QUERY OK db=7.2ms queue=5.3ms idle=633.5ms
UPDATE "users" SET "email_confirmation_token" = $1, "email_confirmed" = $2, "updated_at" = $3 WHERE "id" = $4 [nil, true, ~U[2020-08-19 02:18:18Z], 1]
[debug] update success
[debug] end of confirm_email
[info] Sent 204 in 85ms

The side-effect is that the page is not routing from the caller:

  def confirm_email(conn, %{"token" => token}) do
    Logger.debug("confirm_email token: #{token}")
    case Accounts.confirm_email!(token) do
      {:ok, updated_user} ->
        Logger.debug("updated_user: #{updated_user}")
        conn
        |> Guardian.Plug.sign_in(updated_user)
        |> put_flash(:info, gettext("Welcome %{user_name}!", user_name: updated_user.name))
        |> redirect(to: Routes.page_path(conn, :index))
      {:error, reason} ->
        Logger.debug("error reason: #{reason}")
        json(put_status(conn, 404), %{error: "invalid_token"})
    end
  end

It’s just not clear to me why it doesn’t appear to be returning the tuple I expect.
Michael

Most Liked

cmkarlsson

cmkarlsson

You are returning the result of the last Logger.debug expression which is :ok

If you really wanted the “end of confirm_email” logging you need to return the result after.

  def confirm_email!(user = %User{email_confirmed: false}) do
    confirm = User.changeset_confirm_email(user, true)
    result = case Repo.update(confirm) do
      # Updated with success
      {:ok, updated_user} ->
        Logger.debug("update success")
        {:ok, updated_user}
      {:error, _, reason, _} ->
        Logger.debug("update error")
        Logger.error(reason)
        {:error, reason}
    end
    Logger.debug("end of confirm_email")
    result
  end
maz

maz

I deleted my response by mistake.

You were right, the root problem was the router. I was calling
get("/confirm_email/:token", UserController, :confirm_email) # for json api

and not:

get("/confirm_email/:token", SignupController, :confirm_email)

SignupController:

  def confirm_email(conn, %{"token" => token}) do
    Logger.debug("confirm_email token: #{token}")
    case Accounts.confirm_email!(token) do
      {:ok, updated_user} ->
        conn
        |> Guardian.Plug.sign_in(updated_user)
        |> put_flash(:info, gettext("Welcome %{user_name}!", user_name: updated_user.name))
        |> redirect(to: Routes.page_path(conn, :index))
      {:error, reason} ->
        Logger.debug("error reason: #{reason}")
        json(put_status(conn, 404), %{error: "invalid_token"})
    end
  end

Works fine. Thanks for the help. Guilty of programming while tired.
Leaving this here so others might benefit.

mindok

mindok

The last statement in your function is Logger.debug call, so it’s return will be your return value

Where Next?

Popular in Chat/Questions Top

jsnoble
I come from a javascript background and I just starting learning elixir. I went through the basic tutorials but I want something deeper. ...
New
Chawki
hi,i’m new to programming world i had learned front-end( javascript,react.js) and i wanna learn a back-end programming language i thought...
New
New
AstonJ
It finally feels like I’ve got some time to catch up on my reading - though I am sure lots of people will be wondering the same: what are...
New
Twfo326
As a novice dev I’m trying to keep the curriculum as lean as possible. My requirements are modest: build simple CRUD apps with Phoenix...
New
shansiddiqui94
Hello all, I recently did my first app in Phoenix and Liveview, many thanks to all the users who assisted me. I found that the tutorial ...
New
marciol
Hey, I have very restricted resources and time so I’m trying to understand the best way to learn Liveview in terms of cost/time. The Pra...
New

Other popular topics Top

Qqwy
Update: How to use the Blogs & Podcasts section You can post links to your blog posts or podcasts either in one of the Official Blog...
3271 130579 1222
New
vertexbuffer
Hello, can anybody help here..? I have a list of players and I what to delete an element, but every for loop the list is reverting to ori...
New
joeerl
Hello again - after a longish gap I’ve decided I really must dig into Elixir and see what’s been happening here - so I have a few questio...
New
axelson
This post is a wiki (feel free to hit the edit button near the bottom right of this post to add your own changes!) This post collects co...
239 49134 226
New
gausby
I asked this very same question on twitter and got some interesting feedback, but I thought it would be a good question to ask here as we...
1207 40082 209
New
senggen
Erlang/OTP 25 [erts-13.2.2] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1] 15:22:35.803 [error] gen_event {lager_file_backend...
New

We're in Beta

About us Mission Statement