redpoll
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?
Marked As Solved
kokolegorille
<%= hidden_input :user_id, value: user.id %>
Also Liked
kokolegorille
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.
kokolegorille
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")
kokolegorille
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")
Popular in Discussions
Other popular topics
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #channels
- #elixirconf
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #phoenix_html
- #iex
- #blog-post
- #graphql
- #genstage
- #ai
- #websockets
- #supervisor
- #advent-of-code
- #elixirconf-us
- #distillery
- #processes
- #forms
- #api
- #metaprogramming
- #security
- #performance









