And the corresponding event handler in my controller is this:
def handle_event("save", %{"accounts" => accounts}, socket) do
{:ok, socket}
end
(it’s not returning anything for now, just the socket)
And yet, when I submit it, I get this error:
no function clause matching in NetAdminWeb.AccountsLive.Edit.mount/3
What gives? I have phx-submit="save" set up, so it should trigger handle_event/3, right? But for some reason, it’s triggering mount/2. Any idea what this could be?
You need to return {:noreply, socket} from handle_event/3:
def handle_event("save", %{"accounts" => accounts}, socket) do
{:noreply, socket}
end
Check your console– odds are your callback is being invoked, then the LiveView crashes, then mount is invoked again when the client-side socket automatically reconnects.
Thanks for your reply. I checked my console and put an IO.inspect(accounts, label: "the accounts are here") in before the {:noreply, socket}. However, the page still crashes, mount is still triggered, and there’s no output from IO.inspect. Here’s the stacktrace, if it helps at all:
That is the parameters on mount though, which seems to suggest your parameters are sent as URL/GET parameters. That shouldn’t really be happening for the code you showed.
You’re right. And the arguments are in the wrong order.
Okay, for context, I am migrating this thing from a pure Phoenix app to Liveview. This app was generated using mix phx.new within the past month, so it came with LiveView already installed. Is there any reason it might still be behaving like a Phoenix app rather than a LiveView one? Something in the router, maybe?
On that note, however, it appears that mount/3 IS being called, which I can see via IO.inspect. So apparently the liveview is being mounted, and then crashing right away.
It shouldn’t matter what’s in the liveview module - as @LostKobrakai pointed out, something fishy is going on because the submit button is sending params when it shouldn’t. It’s as if it’s trying to mount the liveview again because something is making it crash.
You mentioned your LV is mounting and then crashing, so something inside it may be making it crash. It’s the first place we could look further to try to help you out, and then move to other parts.
What is the first error you see when you try to access the page? (if Accounting.get_account_!(id) fails, your LV will crash)
Any error on the phx console?
Any JS or other error on the browser console?
This LV has a route defined in your router, right? Or is it inside another LV or controller?
Your “validate” handle_event has the “account” key, your “save” event has an underscore and is “account_” instead. Those defp aren´t used, did you post your entire template previously?
You can try changing your “save” and “validate” events a bit to see what’s getting in there:
def handle_event("validate", params, socket) do
IO.inspect(params, label: "second arg to handle_event/3")
{:noreply, socket}
end
Nothing is going into the “validate” event. The error in the phx console is:
[error] #PID<0.766.0> running NetAdminWeb.Endpoint (connection #PID<0.730.0>, stream id 7) terminated
Server: localhost:4000 (http)
Request: GET /accounts/edit?accounts%5Busername%5D=bob&%5Bpassword%5D=spass&account_accounts%5Bemail_address%5D=email1%40example.com
** (exit) an exception was raised:
** (FunctionClauseError) no function clause matching in NetAdminWeb.AccountsLive.Edit.mount/3
Those other two events aren’t even being called . I’m wondering if this could be related to js or npm issues…
Thank you! This solved the issue, at least partially. I had the following in my app.js:
// Include phoenix_html to handle method=PUT/DELETE in forms and buttons.
import "phoenix_html"
// Establish Phoenix Socket and LiveView configuration.
import {Socket} from "phoenix"
import {LiveSocket} from "phoenix_live_view"
import topbar from "../vendor/topbar"
import jquery from "jquery"
import "../adminlte/js/theme"
import "../adminlte/vendor/bootstrap-notify.min.js"
import "../adminlte/vendor/jquery.min.js"