maz

maz

Plug.Conn shopping cart: is_nil() check renders value nil?

So I have a shopping cart that I tied to the session with this great package:
https://github.com/pentacent/phoenix_live_session

anyway, I’m allowing the user to add to the cart(which is a list of product ids) while they are not logged-in. So when they do log in, I want to put the shopping cart list into the new session:

cart = if(is_nil(cart), do: [])
  def login_user(conn, user, params \\ %{}) do
    cart = get_session(conn, "shopping_cart")
    IO.inspect(cart, label: "UserAuth login_user shopping_cart")

    # cart can be nil if the unlogged-in user didn't add something to the cart
    cart = if(is_nil(cart), do: [])
    IO.inspect(cart, label: "UserAuth login_user shopping_cart")

    token = Accounts.generate_user_session_token(user, params["org_id"])
    user_return_to = get_session(conn, :user_return_to)

    conn
    |> renew_session()
    |> put_session(:user_token, token)
    |> put_session(:live_socket_id, "users_sessions:#{Base.url_encode64(token)}")
    |> put_session("shopping_cart", cart)
    |> maybe_write_remember_me_cookie(token, params)
    |> redirect(to: user_return_to || signed_in_path(conn))

    conn
  end

The above line cart = if(is_nil(cart), do: []) seems make the shopping_cart nil:

UserAuth login_user shopping_cart: ["prod_IpMCdH7VQv1bWz", "prod_IpMCdH7VQv1bWz", "prod_IpMCdH7VQv1bWz"]
UserAuth login_user shopping_cart: nil

Am I missing something here when it comes to how get_session() and put_session() work?

Marked As Solved

benwilson512

benwilson512

Author of Craft GraphQL APIs in Elixir with Absinthe
    cart = if(is_nil(cart), do: [])

This line is equivalent to:

cart = if is_nil(cart) do
  []
else
  nil
end

This is probably not what you want, because if cart is NOT nil then it sets it to nil. From your logic it seems like what you want is:

cart = get_session(conn, "shopping_cart") || []

This will get the value from the session and if it is nil, instead default to [].

Also Liked

maz

maz

Nice idiom, that nailed it. Thanks!

Where Next?

Popular in Questions Top

chokchit
** (DBConnection.ConnectionError) connection not available and request was dropped from queue after 2733ms. You can configure how long re...
New
aadeshere1
I have a another noob question about loop. Since elixir is immutable, while loop is not directly possible. total = 10 while total != 0 ...
New
WestKeys
Currently suffering from paralysis by [HTTP client] analysis. This is rather unusual in Elixirland as there tends to be consensus on the ...
New
jononomo
I am trying to figure out how Mix knows whether the environment is test, dev, or prod – where is this set? Thanks.
New
stefanluptak
Hello everybody, usually, I use a 29" ultra-wide monitor for VSCode which can easily accomodate explorer (files panel) + file with code ...
New
ycv005
I have followed this StackOverflow post to install the specific version of Erlang. And When I am running mix ecto.setup then getting fol...
New
jay1
Why is it that the mnesia database isn’t the most preferred database for use in Elixir/Phoenix?
New
bsollish-terakeet
Credo is smart enough to check for (something like) this: assert length(the_list) == 0 with this response: Checking if an enum is empt...
New
PeterCarter
There are pre-rolled solutions for other frameworks that do work. However, Phoenix does not seem to have these. Have people had good expe...
New
JorisKok
I have a server on AWS, and was running a load test using artillery. When looking at the Phoenix dashboard I see the Ports going to 100% ...
New

Other popular topics Top

Darmani72
If I have a post route which an argument: post /my_post_route/:my_param1, MyController.my_post_handler How would get the post params ...
New
albydarned
Hello all! I am typing this post from my new MacBook Pro with the M1 chip. I’m loving it so far, and will probably use it as my daily dr...
New
minhajuddin
I have seen a lot of code which picks the first element from a list using Enum.at(0) instead of List.first. Is there a reason why people ...
New
fireproofsocks
Forgive me if this is obvious, but how does one delete a database record WITHOUT selecting it first? Ecto.Repo — Ecto v3.14.0 has exampl...
New
belgoros
I’m not a pro in using Regex and can’t figure out why the following behaviour happens, especially if we take into account the difference ...
New
vrod
I am using the Starship cross-shell prompt – it seems pretty nice, but I get some errors: [WARN] - (starship::utils): Executing command ...
New
AstonJ
Please see the new poll here: Which code editor or IDE do you use? (Poll) (2022 Edition) It’s been a while since we first asked this, I...
208 31265 143
New
saif
Hello everyone, Long time lurker first time poster here. I’ve recently begun working on Elixir full-time again! :raised_hands: It’s been...
New
shijith.k
I am trying to start a new phoenix project with elixir 1.9, but mix phx.new does not work. It says that ** (Mix) The task "phx.new" could...
New
vegabook
I’m brand new to Phoenix and I have stripped one of the demo applications to the bone. I just want to get an svg up on the screen. Here i...
New

Latest on Elixir Forum

Elixir Forum

We're in Beta

About us Mission Statement