marcz2007
No function clause matching UserController.create Method when POSTing
I am trying to post a test user account to the users table in the repo but I am getting the ‘no function clause matching’ error for some reason.
I have tried POSTing on both curl and Postman with the same results. Here is my post using CURL:
curl -H "Content-Type: application/json" -X POST -d '{"name":"test","id":"5"}' http://localhost:4000/api/users2
Another trial of CURL (in the style of the tutorial I am using - https://pamit.medium.com/building-a-restful-backend-with-elixir-phoenix-84fe390975c - uses):
curl -X POST http://localhost:4000/api/users2 -H “Content-Type: application/json” -d ‘{“accounts”: {“name”: “test”, “id”: “5"}}’
In Postman I am writing the URL with the JSON in the body:
http://localhost:4000/api/users2
{"name":"test","id":"5"}
Here is my create method which should be matching in order to add the new user account:
def create(conn, %{"user" => user_params}) do
with {:ok, %User{} = user} <- Accounts.create_user(user_params) do
conn
|> put_status(:created)
|> put_resp_header("location", Routes.user_path(conn, :show, user))
|> render("show.json", user: user)
end
end
Marked As Solved
marcz2007
SOLVED: Inside the body of the HTML post on Postman, you need to include the exact variable name that the controller function uses (in my case this is ‘user’).
{“user”:{“name”:“test2”,“amount”:“5.0”}}
^^ This worked!
Thanks all who helped
Also Liked
kokolegorille
It does not look like the code in the blog You are following… which looks like this
def create(conn, %{"business" => business_params}) do
with {:ok, %Business{} = business} <- Directory.create_business(business_params) do
conn
|> put_status(:created)
|> put_resp_header("location", business_path(conn, :show, business))
|> render("show.json", business: business)
end
end
Anyway, I would not recommend to follow this (old) post because things have changed since.
Maybe try this one, which cover Phoenix 1.5
https://lobotuerto.com/blog/building-a-json-api-in-elixir-with-phoenix/
BTW You are using user variable, which was never defined, and You define user_params, which is never used.
axelson
Your create function is not matching the parameters that you are sending via curl. A possible fix is to change the first line from:
def create(conn, %{"user" => user_params}) do
to:
def create(conn, %{"accounts" => user_params}) do
Although personally i prefer to do little to no matching in controller function heads because I find the no function clause matching not very informative. If you changed your create to function to:
def create(conn, params) do
%{"user" => user_params} = params
...
end
Then you would have gotten a more informative pattern matching error.
Popular in Questions
Other popular topics
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #channels
- #elixirconf
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #phoenix_html
- #iex
- #blog-post
- #graphql
- #genstage
- #ai
- #websockets
- #supervisor
- #elixirconf-us
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #metaprogramming
- #security
- #hex









