How do i fix this error using Elixir 1.82

[error] GenServer #PID<0.509.0> terminating
** (FunctionClauseError) no function clause matching in Ecto.build_assoc/3
    (ecto) lib/ecto.ex:461: Ecto.build_assoc(nil, :comments, %{})
    (discuss) lib/discuss_web/channels/comments_channel.ex:18: DiscussWeb.CommentsChannel.handle_in/3
    (phoenix) lib/phoenix/channel/server.ex:279: Phoenix.Channel.Server.handle_info/2
    (stdlib) gen_server.erl:637: :gen_server.try_dispatch/4
    (stdlib) gen_server.erl:711: :gen_server.handle_msg/6
    (stdlib) proc_lib.erl:249: :proc_lib.init_p_do_apply/3
Last message: %Phoenix.Socket.Message{event: "comment:add", join_ref: "1", payload: %{"content" => "nmbb,mn"}, ref: "2", topic: "comments:1"}
State: %Phoenix.Socket{assigns: %{topic: %Discuss.Topic{__meta__: #Ecto.Schema.Metadata<:loaded, "topics">, comments: #Ecto.Association.NotLoaded<association :comments is not loaded>, id: 1, title: "Ger", user: #Ecto.Association.NotLoaded<association :user is not loaded>, user_id: 1}}, channel: DiscussWeb.CommentsChannel, channel_pid: #PID<0.509.0>, endpoint: DiscussWeb.Endpoint, handler: DiscussWeb.UserSocket, id: nil, join_ref: "1", joined: true, private: %{log_handle_in: :debug, log_join: :info}, pubsub_server: Discuss.PubSub, ref: nil, serializer: Phoenix.Socket.V2.JSONSerializer, topic: "comments:1", transport: :websocket, transport_pid: #PID<0.502.0>}
[debug] QUERY OK source="topics" db=17.5ms queue=0.4ms
SELECT t0."id", t0."title", t0."user_id" FROM "topics" AS t0 WHERE (t0."id" = $1) [1]
[info] JOINED comments:1 in 19ms
  Parameters: %{}

There is no elixir 1.82, therefor I assume you are talking about 1.8.2.

Anyway, without seeing your code its hard to say…

Can you please post some code? The function clause around lib/discuss_web/channels/comments_channel.ex, line 18 seems to be interesting from the stack trace.


You passed nil into the build_assoc function when you should have passed a struct. In your comments_channel.ex file you probably had something along the lines of

post = DiscussWeb.Repo.get(DiscussWeb.Post, post_id)

and that returned nil because the post_id does not exist.

1 Like