No function clause matching in Access.get/3

368 Views Asked by At

In my free time I am learning elixir, and now I am trying to develop simple Elixir API. Everything works fine, besides when I try to insert new values using Postman/curl. When I'm trying to do so with such data:

{
    "tree": "nil",
    "value": "5"
}

I get this error: (FunctionClauseError) no function clause matching in Access.get/3.

Here is my code for POST in my Endpoint.

post "/insert" do
    IO.inspect(conn.body_params)
    {status, body} =
      case conn.body_params do
        %{"tree" => tree, "value" => value} -> {200, process_insert(tree, value)}
        BstInsertion.insert(tree, value)
        _ -> {400, missing_insert()}
      end

    send_resp(conn, status, body)
  end

Example of insert function and process_insert:

  def insert(nil, value) do
    %{value: value, ltree: nil, rtree: nil}
  end

  defp process_insert(_tree, _value) do
    Poison.encode!(%{response: "Received insert!"})
  end

If this is not enough, I will provide more code. Thank you for any advice and help.

1

There are 1 best solutions below

0
vfsoraki On

I think you have a problem in your code. I have tidied up your code a bit:

post "/insert" do
  IO.inspect(conn.body_params)
  {status, body} =
    case conn.body_params do
      %{"tree" => tree, "value" => value} ->
        {200, process_insert(tree, value)}
        BstInsertion.insert(tree, value) # What is this line? What is the output?
        
      _ ->
        {400, missing_insert()}
    end

  send_resp(conn, status, body)
end

The last line in each clause/function is the return value. So your first case clause returns the value of BstInsertion.insert(tree, value). Is that what you expect? From the previous line, I can say it is not.