Rendering JSON: no match of right hand side value

Hi, can someone help me point out, what causes this error

[error] #PID<0.616.0> running SentiWeb.Endpoint (connection #PID<0.559.0>, stream id 5) terminated
Server: localhost:4000 (http)
Request: POST /api/presentations/session
** (exit) an exception was raised:
    ** (MatchError) no match of right hand side value: 68
        (cowboy 2.8.0) /home/hei/projects/senti/deps/cowboy/src/cowboy_req.erl:815: :cowboy_req.reply/4
        (plug_cowboy 2.5.0) lib/plug/cowboy/conn.ex:35: Plug.Cowboy.Conn.send_resp/4
        (plug 1.11.1) lib/plug/conn.ex:410: Plug.Conn.send_resp/1
        (senti 0.1.0) lib/senti_web/controllers/presentation_session_controller.ex:1: SentiWeb.PresentationSessionController.action/2
        (senti 0.1.0) lib/senti_web/controllers/presentation_session_controller.ex:1: SentiWeb.PresentationSessionController.phoenix_controller_pipeline/2
        (phoenix 1.5.8) lib/phoenix/router.ex:352: Phoenix.Router.__call__/2
        (senti 0.1.0) lib/senti_web/endpoint.ex:1: SentiWeb.Endpoint.plug_builder_call/2
        (senti 0.1.0) lib/plug/debugger.ex:136: SentiWeb.Endpoint."call (overridable 3)"/2
        (senti 0.1.0) lib/senti_web/endpoint.ex:1: SentiWeb.Endpoint.call/2
        (phoenix 1.5.8) lib/phoenix/endpoint/cowboy2_handler.ex:65: Phoenix.Endpoint.Cowboy2Handler.init/4
        (cowboy 2.8.0) /home/hei/projects/senti/deps/cowboy/src/cowboy_handler.erl:37: :cowboy_handler.execute/2
        (cowboy 2.8.0) /home/hei/projects/senti/deps/cowboy/src/cowboy_stream_h.erl:300: :cowboy_stream_h.execute/3
        (cowboy 2.8.0) /home/hei/projects/senti/deps/cowboy/src/cowboy_stream_h.erl:291: :cowboy_stream_h.request_process/3
        (stdlib 3.14.1) proc_lib.erl:226: :proc_lib.init_p_do_apply/3

In my controller, if an id does not exist in the table, it creates the record otherwise it returns the existing record.

  def create(conn, params) do
    with {:ok, %Session{} = session} <- Presentations.create_session(params) do
      conn
      |> put_status(204)
      |> render("session.json", session: session)
    else
      session ->
        conn
        |> put_status(200)
        |> render("session.json", session: session)
    end
  end

and here’s my context module:

  def create_session(attrs \\ %{}) do
    case get_presentation(attrs["presentation_id"]) do
      nil ->
        {:error, :not_found}      presentation ->
        case get_session(presentation.id) do
          nil ->
            %Session{}
            |> Session.changeset(attrs)
            |> Repo.insert()          
          session ->
            session
        end
    end
  end

The error only happens when new record is created.

I would guess that you are sending a status code 204, which means “no content” and then sending content with the render.

1 Like

That’s it. It should be put_status(:created).

Thank you.

1 Like