Why the value of an id is nil?

I have a form in heex

<form phx-submit="add_points" >

        <%= number_input :user_points, :points, phx_value_user_id: user.id %>
        <%= submit "Add" %>

      </form>

and a handle event (2 actually):

  def handle_event("add_points", %{"user-id" => user_id}, socket) do
    {:noreply, assign(socket, :user_id, user_id)}
  end

  def handle_event("add_points", %{"user_points" => %{"points" => points_for_user}}, socket) do
    user_id = socket.assigns[:user_id]
    post = socket.assigns[:post]
           |> Repo.preload(:users)
    Events.add_user_points(post, user_id, points_for_user)
    {:noreply, socket}
  end

The problem is that I get an ** (ArgumentError) comparison with nil is forbidden as it is unsafe. If you want to check if a value is nil, use is_nil/1 instead error when using the user_id in a query:

  def add_player_points(%Post{} = post, user_id, points_for_user) do
    pp = post.id
    query = from(pu in PostUser, where: pu.post_id == ^pp and pu.user_id == ^user_id) #here
...

I have tried also this in one handle event:

  def handle_event("add_points", %{"user_points" => %{"points" => points_for_user}}, socket) do
    user_id = "user-id"
    post = socket.assigns[:post]
           |> Repo.preload(:users)
    Events.add_user_points(post, user_id, points_for_user)
    {:noreply, socket}
  end

also

user_id = String.to_integer("user-id") 

Without luck…

What should I do?

Is that a typo?

if user_id is null, database will not be happy.

You cannot ask DB for id == null, if You really want to check, You must use IS_NULL()

Maybe ensure first the user_id is present before sending the query.

1 Like

Yeah, sorry for the typo.

The problem is that user_id is NOT null for sure, inside a

<%= for user <- @users do %>
<%= user.id %>

it renders me 5 users, 5 ids. In an another association, many_to_many, I use them successfully, so they are not null :slight_smile:

Just check it’s not nil here, You never know, because You might have typos before

In particular, the last one… are You sure You want this?

user_id = socket.assigns[:user_id] |> IO.inspect(label: "===> ID")
1 Like

thanks… from my research I came to the conclusion that user.id from heex didn’t passed to the live ex.
In my form I have

<%= number_input :user_points, :points, phx_value_user_id: user.id %>

but since handle event is /3 I can’t do the map:

def handle_event("add_points", %{"user_points" => %{"points" => points_for_user}, "user_id" => user_id}, socket) do

(FunctionClauseError) no function clause matching in AppWeb.PostLive.handle_event/3

This should be easy to debug… Add a catch all at last position, and see what You get.

def handle_event("add_points", message, socket), do: IO.inspect(message, label: "MESSAGE")
1 Like

yeah, I got

==========> MESSAGE: %{"user_points" => %{"points" => "2"}}

there’s no user_id…

This looks wrong.

Maybe just use a hidden_field to store user id value.

1 Like

I’m so sorry, but I got really confused… As I understand, we cannot use phx-value in a form event? When doing

<%= hidden_input :user_id, :user_id %>

I got the message: ==========> MESSAGE: %{"user_id" => %{"user_id" => ""}, "user_points" => %{"points" => "2"}}, but how should I set up the value of that user_id? (eg. phx_value: user.id)?

<%= hidden_input :user_id, value: user.id %>
1 Like

I understand, thank you!