marcz2007

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

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

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

axelson

Scenic Core Team

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.

Where Next?

Popular in Questions Top

vegabook
I’m brand new to Phoenix and I have stripped one of the demo applications to the bone. I just want to get an svg up on the screen. Here i...
New
RisingFromAshes
I’ve read in another post that it may be possible with a router helper - but I couldn’t find an appropriate one, and tbh, I’m still just ...
New
hariharasudhan94
I would like to know what is the best IDE for elixir development?
New
skosch
To my knowledge, put_in, Map.update etc. all have the one limitation of not automatically creating intermediate keys when needed (for exa...
New
mcarvalho
What is the difference between System.get_env and Application.get_env? For example, what are best practices to use one versus another.
New
aadeshere1
I have a another noob question about loop. Since elixir is immutable, while loop is not directly possible. total = 10 while total != 0 ...
New
sergio_101
I am VERY much an elixir newbie. I have taken one elixir course and one phoenix course on Udemy. During that course, I saw the instructor...
New

Other popular topics Top

Qqwy
Update: How to use the Blogs &amp; Podcasts section You can post links to your blog posts or podcasts either in one of the Official Blog...
3271 130579 1222
New
joaquinalcerro
Hi there, I am working with Ecto-Postgresql and I need to call all of the records from a specific table but the table has 40,000 records...
New
New
joeerl
Hello again - after a longish gap I’ve decided I really must dig into Elixir and see what’s been happening here - so I have a few questio...
New
gausby
I asked this very same question on twitter and got some interesting feedback, but I thought it would be a good question to ask here as we...
1207 40082 209
New
albydarned
Hello all! I am typing this post from my new MacBook Pro with the M1 chip. I’m loving it so far, and will probably use it as my daily dr...
New

We're in Beta

About us Mission Statement