Function populating list when ran from 'iex -S mix' not when running inside the app

Greetings Everyone!!!

I am so desperate now. This path starting with Web Development using Elixir/Phoenix has been kind of painful since I have been having a lot of small issues, and right now I have no idea about where to run to fix this.

Problem

When I run a function from the iex -S mix session, it returns the expected list, but … when that same functions is called inside the app, it is returning an empty list.

Details

This is the code where I am calling the mentioned function. Lots of traces and two versions of the same function: position_opts and position_opts1, just to figure out what am I doing wrong, but I failed.

  defp apply_action(socket, :new, _params) do
    IO.puts " ::: before ::: "
    position_opts = Management.position_opts()
    IO.puts " ::: after ::: "

    IO.puts """
    ::: apply_status new position_opts ::::::::::::::::::::::::::::::::::::::::
    position_opts: #{inspect(position_opts, limit: :infinity)}
    ::: apply_status new position_opts ::::::::::::::::::::::::::::::::::::::::
    """

    position_opts = Management.position_opts1()

    IO.puts """
    ::: apply_status edit position_opts1 ::::::::::::::::::::::::::::::::::::::::
    position_opts: #{inspect(position_opts, limit: :infinity)}
    ::: apply_status edit position_opts1 ::::::::::::::::::::::::::::::::::::::::
    """

    socket
    |> assign(:page_title, "New Employee")
    |> assign(:position_opts, position_opts)
    |> assign(:employee, %Employee{})
  end
# === .../management.ex  ===
  def position_opts() do
    IO.puts ":::: Running position_opts ::::::::"
    for pos <- list_positions() do
      [ key: pos.pos_descrip, value: pos.id ]
    end
  end
  def position_opts1() do
    IO.puts ":::: Running position_opts1 ::::::::"
    list_positions()
    |> Enum.map(fn pos -> [ key: pos.pos_descrip, value: pos.id ] end)
  end

Here is the console output when the app is running. I can see the same output twice, since, as far as I understand, the mount function runs twice. As you can see, it is showing the traces when both functions are running, and then the empty list.

[info] GET /surtt/employees/new
[debug] Processing with SurttWeb.EmployeeLive.Index.new/2
  Parameters: %{}
  Pipelines: [:browser, :surtt_pipe]
::::::::::::::::: checking you :::::::::::::::::::::::::::
 ::: before ::: 
[debug] QUERY OK source="employees" db=0.4ms idle=1688.1ms
SELECT e0."id", e0."emp_active", e0."emp_address", e0."emp_birth", e0."emp_city", e0."emp_colony", e0."emp_country", e0."emp_county", e0."emp_curp", e0."emp_finish", e0."emp_lastname", e0."emp_firstname", e0."emp_phone", e0."emp_postal_code", e0."emp_rfc", e0."emp_seclname", e0."emp_sex", e0."emp_start", e0."emp_state", e0."emp_username", e0."emp_password", e0."emp_old_password", e0."emp_position", e0."inserted_at", e0."updated_at" FROM "management"."employees" AS e0 []
↳ SurttWeb.EmployeeLive.Index.mount/3, at: lib/surtt_web/live/employee_live/index.ex:9
:::: Running position_opts ::::::::
 ::: after ::: 
[debug] QUERY OK source="positions" db=0.8ms idle=1688.6ms
SELECT p0."id", p0."pos_active", p0."pos_descrip", p0."inserted_at", p0."updated_at" FROM "management"."positions" AS p0 WHERE (p0."pos_active" = TRUE) ORDER BY p0."pos_descrip" []
↳ Surtt.Management.position_opts/0, at: lib/surtt/management.ex:211
::: apply_status new position_opts ::::::::::::::::::::::::::::::::::::::::
position_opts: []
::: apply_status new position_opts ::::::::::::::::::::::::::::::::::::::::

:::: Running position_opts1 ::::::::
::: apply_status edit position_opts1 ::::::::::::::::::::::::::::::::::::::::
position_opts: []
::: apply_status edit position_opts1 ::::::::::::::::::::::::::::::::::::::::

[debug] QUERY OK source="positions" db=0.0ms idle=1689.6ms
SELECT p0."id", p0."pos_active", p0."pos_descrip", p0."inserted_at", p0."updated_at" FROM "management"."positions" AS p0 WHERE (p0."pos_active" = TRUE) ORDER BY p0."pos_descrip" []
↳ Surtt.Management.position_opts1/0, at: lib/surtt/management.ex:217
::: assigns.position_opts ::::::::::::::::::::::::::::::::::::::::
assigns.position_opts: 
::: assigns.position_opts ::::::::::::::::::::::::::::::::::::::::

[info] Sent 200 in 2ms
[info] CONNECTED TO Phoenix.LiveView.Socket in 42µs
  Transport: :websocket

But, when I run the same function from an iex -S mix session, it populates the list just fine, and … as you might notice, the trace inside the function running is also showing up.

iex(1)> Main.Management.position_opts()
:::: Running position_opts ::::::::
[debug] QUERY OK source="positions" db=3.1ms decode=0.7ms queue=0.8ms idle=924.9ms
SELECT p0."id", p0."pos_active", p0."pos_descrip", p0."inserted_at", p0."updated_at" FROM "management"."positions" AS p0 WHERE (p0."pos_active" = TRUE) ORDER BY p0."pos_descrip" []
↳ Surtt.Management.position_opts/0, at: lib/surtt/management.ex:211
[
  [key: "Store Room", value: 1],
  [key: "Packing", value: 2],
  [key: "Supervisor", value: 3]
]
iex(2)> Main.Management.position_opts1()
:::: Running position_opts1 ::::::::
[debug] QUERY OK source="positions" db=1.3ms idle=1053.2ms
SELECT p0."id", p0."pos_active", p0."pos_descrip", p0."inserted_at", p0."updated_at" FROM "management"."positions" AS p0 WHERE (p0."pos_active" = TRUE) ORDER BY p0."pos_descrip" []
↳ Surtt.Management.position_opts1/0, at: lib/surtt/management.ex:217
[
  [key: "Store Room", value: 1],
  [key: "Packing", value: 2],
  [key: "Supervisor", value: 3]
]
iex(3)> 

Any help will be more than welcome!!!

Best regards,

Greg

Hello again GregPhx :smiley:

I think we’re going to need a little bit more context. The function in question makes a db call, ie, has a side-effect, which means it’s nearly impossible to decipher just from the code you shared. The database is simply returning results in one case and not in the other. With this my mind goes directly to thinking you are running them in different envs. When you say “inside the app” do you perchance mean when you run tests? Or are you perhaps doing MIX_ENV=prod mix phx.server or any combination of that sort of thing? It’s possible I’m missing a key detail, but I’ve been staring at this for like 10 mins :sweat_smile:

Otherwise, can you share the list_positions/0 function?

1 Like

You are a genius essseeee!!!

I have been working all day long, and barely started making progress showing some data. I changed my environment very early this morning and I thought I had reloaded the new environment in all my terminal sessions. Turns out … I missed the one where I run mix phx.server.

  username: System.get_env("APP_USER"),
  password: System.get_env("APP_PASS"),
  hostname: System.get_env("APP_HOST"),
  database: System.get_env("APP_DB"),

I am feeling so stupid once again. I do apologize for the inconvenience.

Best regards,

Greg.

1 Like