Programming Phoenix LiveView: assign_demographic issue p179

Hi all,

I’m getting this when going to /survey and all code appears to be the same:

Request: GET /survey
** (exit) an exception was raised:
    ** (FunctionClauseError) no function clause matching in PentoWeb.DemographicLive.Form.assign_demographic/1
        (pento 0.1.0) lib/pento_web/live/demographic_live/form.ex:16: PentoWeb.DemographicLive.Form.assign_demographic(#Phoenix.LiveView.Socket<assigns: %{__changed__: %{flash: true, id: true, user: true}, flash: %{}, id: "demographic-form", myself: %Phoenix.LiveComponent.CID{cid: 1}, user: #Pento.Accounts.User<__meta__: #Ecto.Schema.Metadata<:loaded, "users">, confirmed_at: nil, email: "ghenry@suretec.co.uk", id: 2, inserted_at: ~N[2022-05-27 09:51:04], updated_at: ~N[2022-05-27 09:51:04], username: "", ...>}, endpoint: PentoWeb.Endpoint, id: "phx-FxEBqzi2O1ybsgYI", parent_pid: nil, root_pid: nil, router: PentoWeb.Router, transport_pid: nil, view: PentoWeb.SurveyLive, ...>)
        (pento 0.1.0) lib/pento_web/live/demographic_live/form.ex:11: PentoWeb.DemographicLive.Form.update/2
        (phoenix_live_view 0.17.10) lib/phoenix_live_view/utils.ex:394: Phoenix.LiveView.Utils.maybe_call_update!/3
        (phoenix_live_view 0.17.10) lib/phoenix_live_view/diff.ex:608: anonymous fn/5 in Phoenix.LiveView.Diff.render_pending_components/6
        (elixir 1.13.4) lib/enum.ex:2396: Enum."-reduce/3-lists^foldl/2-0-"/3
        (stdlib 3.17.2) maps.erl:410: :maps.fold_1/3

Any pointers?

Thanks.

Logged in with wrong user…

but logging in with another user give the above error. Why is that?

Yeah, there’s some issue here as p181 states:

Now if we log in a user that does not have an associated demographic record
and visit /survey, we should see our survey page, including the demographic
form, as shown here.

Got some help in Elixir slack channels. Seems to be when a user hasn’t added a survey etc. via an iex session, it’s trying to match on the user: key, not current_user:

I’ll see where that was changed.

Found the issue:

Page 181 in the PDF has the code like above, but line 9 should be the key current_user like is shows later in the PDF on page 191 (current_user={@current_user})

Great learning experience though!

Excellent detective work! Thanks so much for catching this and sorry if it sent you down a rabbit hole :slight_smile: I’ll fix this for the next release.

1 Like