redpoll

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

kokolegorille

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

Also Liked

kokolegorille

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

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

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")

Where Next?

Popular in Discussions Top

blackode
Elixir Upgrading is so Simple in Ubuntu and It worked for me Ubuntu 16.04 git clone https://github.com/elixir-lang/elixir.git cd elixir...
New
mikl
I wanted to capitalize a string, and tried using String.capitalize(). That generally works well, until you try to capitalize a word like...
New
rower687
Hi all, I’ve been reading a lot about the “let it crash” term and how supervising processes and the whole messaging passing make an elixi...
New
und0ck3d
Hello everyone! A few days ago I’ve created a topic here about how people were creating CMSs with Elixir and Phoenix. I’ve been studying...
New
cblavier
Hey there, It’s been more than a year since we started using LiveView as our main UI library and building a whole library of UI componen...
New
wmnnd
The Go vs Elixir thread got me thinking: Would it be too hard to implement a simple mechanism for creating Go-style static app binaries f...
New
scouten
I’m looking for a host for the server part of a small (personal) side project that I’m working on. It’s currently written in Node.js and ...
New
JakeBecker
TL;DR: I’ve just released an implementation of Microsoft’s IDE-independent Language Server Protocol for Elixir. It adds language support ...
1144 54120 245
New
sashaafm
Piggy backing a bit on @dvcrn topic BEAM optimization for functions with static return type?, I’ve been trying to understand in a deeper ...
New
AstonJ
It’s been a while since we’ve had a thread like this, so what better way to kick off the year with :003: What does being an Elixir user ...
New

Other popular topics Top

marius95
Hello everyone, I try to use an Javascript Event Handler in my root.html.leex file. Therefore I created a function in the app.js file: ...
New
baxterw3b
Hi guys, i’m new in the Elixir world, and i have to say, that i love it! i’m having some problem to understand anonymous functions with ...
New
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
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 Postgres...
New
Fl4m3Ph03n1x
About me? ( if you have nothing better to do than reading about some random guy in the internet :stuck_out_tongue: ) Hello all, this is ...
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
chrismccord
This release brings a number of exciting features, including integration with the new Phoenix LiveDashboard and Phoenix LiveView. There h...
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
freewebwithme
Using vs code and installed ElixirLS: support and debugger. And I got an error popped up on start up says Failed to run ‘elixir’ comma...
New
nsuchy
Hi. I’ve noticed that Windows Powershell has it’s own IEX command and you cannot access Elixir’s IEX due to the conflict. This isn’t a cr...
New

Latest on Elixir Forum

Elixir Forum

We're in Beta

About us Mission Statement