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

Iex.new
Hello!, I just started this week to discover Elixir. I’m a PHP-Programmer and did some sutff in Go too. The more I read about Elixir t...
New
xgilarb
Hi there, I’m interested in using Elixir because of the rumors about the reliability of the Phoenix framework, and surprisingly, Elixir’...
New
Emily
Preface: I’m not sure if thise is the right place, because this is not direclty Elixir related… but I’ve always got some of the best advi...
New
markdev
What are the best beginner resources for learning Elixir and OTP (not Phoenix) in 2018?
New
maqbool
what books/Resources do you recommend to learn about distributed system(theory)?
New
tom_s
Hello Elixir Community! I’m new to functional programming in general and to Elixir in particular but I’m very intrigued and would like t...
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
dogweather
Can anyone recommend books/courses/videos that use real-world Elixir? E.g.: Idiomatic error handling design, whether it’s {ok/error, .....
New
Fl4m3Ph03n1x
Background Hey guys, recently I bought a book on TDD that I am reading. The books is really nice and has some really juicy things on arch...
New

Other popular topics Top

gshaw
What is the idiomatic way of matching for not nil in Elixir? E.g., First way: defp halt_if_not_signed_in(conn, signed_in_account) when...
New
_russellb
I want to try my hand at web scraping. What tools/libraries do I need to use. I’m hoping to turn this into something professional so don’...
New
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
New
fireproofsocks
I’m working on defining a simple Ecto schema for a table (in PostGres), but I don’t see where I can define a column as NOT NULL. Conside...
New
stefanchrobot
What’s the safe way to decode a JSON string into a struct? I want to avoid calling String.to_atom. Jason.decode can give me a map with st...
New
mgjohns61585
Could someone help me? I'm making my first elixir program, number guessing game. I can't figure out how to convert the user's guess from ...
New
ashish173
I am using Ecto timestamps with postgres, I can see the timestamps() use the :naive_dateime but for my use case I wanted to store the ti...
New
johnnyicon
Hi all, I've just started learning Elixir and Phoenix Framework, so please pardon my n00bness at this stage. I'm trying to use Postg...
New
josevalim
Hi everyone, One of the features added to Elixir early on to help integration with Erlang code was the idea of overridable function defi...
New
AstonJ
We’ve put together this wiki for Phoenix LiveView - please feel free to add any info you feel is worth including. What is Phoenix LiveV...
New

We're in Beta

About us Mission Statement