Error: Could not find matching clause to process request

I am trying to hit get api for getting the values of cash position… it says

could not find a matching WebApi.Web.CashPositionController.get_cash_position clause to process request. This typically happens when there is a parameter mismatch but may also happen when any of the other action arguments do not match.

def get_cash_position(%{assigns: %{login_id: login_id}} = conn, %{"client_id" => client_id}) do
  with {:ok, cash_positions} <- Funds.get_cash_position(client_id) do
  conn
  |> render(SuccessView, "success.json", %{data: cash_positions, message: ""})
  end
end

This is the Funds.get_cash_position fn

 def get_cash_position!(id), do: Repo.get!(CashPosition, id)

so not understanding where error is…it says clause doesnt matching…but i have two functions written.

In your controller, the get_cash_position is defined to match only if the first argument is a map that at least has the key login_id and the second argument is a map that at least has the key "client_id". If it’s called with arguments that don’t match those arguments it will fail to match and you get that error.

So if you make a request to your API and forget to send "client_id", that’s the error you get. If you’ve forgotten to assign login_id to the request, you get the same error.

You can try reducing your requirements and printing what you get, you’ll be able to see what is missing.

def get_cash_position(conn, params) do
  IO.puts("assigns = #{inspect(conn.assigns)}")
  IO.puts("params = #{inspect(params)}")

  with {:ok, cash_positions} <- Funds.get_cash_position(Map.get(params, "client_id")) do
  conn
  |> render(SuccessView, "success.json", %{data: cash_positions, message: ""})
  end
end

Also, note that the error specifies that the function called was WebApi.Web.CashPositionController.get_cash_position. This is not the same function as Funds.get_cash_position, even if they have the same name. They are in different modules.

2 Likes

Thank you for the clarification