gpreston

gpreston

LiveView: (ArgumentError) assign @count not available in eex template

I have a LiveView with the following code:

defmodule Webcandy2LocalWeb.CounterLive do
  use Phoenix.LiveView

  def mount(_session, socket) do
    socket = assign(socket, :count, 0)
    {:ok, socket}
  end

  def render(assigns) do
    ~L"""
    <h1>Count: <%= @count %></h1>
    <button>+</button>
    <button>-</button>
    """
  end
end

When I try to access the page for this LiveView, I get the following error:

Request: GET /counter
** (exit) an exception was raised:
    ** (ArgumentError) assign @count not available in eex template.

Please make sure all proper assigns have been set. If this
is a child template, ensure assigns are given explicitly by
the parent template as they are not automatically forwarded.

Available assigns: [:flash, :live_action, :live_module, :socket]

I don’t see why this is happening because I assign :count in the mount/2 function. Is there something I’m missing?

Marked As Solved

ityonemo

ityonemo

mount/2 is now mount/3, depending on your version of phoenix liveview.

Also Liked

ityonemo

ityonemo

ok i’m flying totally in the dark here, so huge props to me if I get this right, lol.

seems like your liveview is mounting only over html and not over the socket. Check to make sure your /js/app.js is after your crsf_meta_tag() function call in the app.html.eex. And it might help? (probably not), to have that be after the render call. The recommended places to put these swapped not to long ago IIRC.

You can generally check to see if it’s mounted by placing some sort of sentinel inside the “mount” call. Usually I like to self() |> IO.inspect(label: "foo") and watch two distinct PIDs fly by in my console when I mount. If you see only one, it’s a sure sign that the websocket (second) mount didn’t happen.

Also for things that are not customer-facing (like operator dashboards) it’s good to put a sentinel inside of your assigns, I have a @live assign which is true when on the second mount, that makes it say [live] or [not live] as expected. Liveness can be ascertained using the connected?/1 function inside your mount.

sreyansjain

sreyansjain

Which version of live view are you using. If you are using v0.10.0 or above please check this

Live view layouts have changed and it might so happen that your app.js is not being called at all.

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
nobody
How to bind a phoenix app to a specific ip address? could not find anything about that, nowhere, unfortunately, but for me this is quite...
New
sergio
In Ruby, I can go: User.find_by(email: "foobar@email.com").update(email: "hello@email.com") How can I do something similar in Elixir? ...
New
siddhant3030
Hi, I have to write a raw query for one of my project. But till now I have used ecto queries and don’t have much experience writing raw ...
New
qwerescape
Is there a way to get the call stack or stack trace at any point in the code? Not from exceptions, but an expression that returns how the...
New
shahryarjb
Hello, I have map which I want to convert it to string like this: the map: %{last_name: "tavakkoli", name: "shahryar"} the string I ne...
New
stefanchrobot
What’s the safe way to decode a JSON string into a struct? I want to avoid calling String.to_atom. Jason.decode can give me a map with st...
New
itssasanka
Hi all, Trying to get some more clarity over utc_datetime and naive_datetime for Ecto: The documentation above suggests that while ...
New
romenigld
I am trying to run a deploy with docker and I successfully runned with this command: docker build -t romenigld/blog-prod . but when I t...
New
dotdotdotPaul
Okay, I’m having a heck of a time trying to figure out how to best handle the validation of belongs_to associations in Ecto. I’m sure I’...
New

Other popular topics Top

TunkShif
This post is an instruction guide to help you setup your Neovim for Elixir development from scratch. It includes general information on h...
274 42158 114
New
9mm
I am constructing a JSON object (map) and I need to conditionally set a field. I’m trying to write proper elixir-way code… and I’m at a l...
New
New
stefanchrobot
What’s the safe way to decode a JSON string into a struct? I want to avoid calling String.to_atom. Jason.decode can give me a map with st...
New
aesmail
Hello guys, I have finally made it. I created an admin interface for a framework. It’s been on my todo list for years and with the curre...
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
hariharasudhan94
Lets say I have map like this fetching from my database %{"_id" =&gt; #BSON.ObjectId&lt;58eb1a7a9ad169198c3dXXXX&gt;, "email" =&gt; ...
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 54250 245
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
jononomo
For some reason my phoenix channels are working for me in my local dev environment, but as soon as I deploy via Docker, I get a 403 error...
New

We're in Beta

About us Mission Statement