List_products/1 is undefined or private

I got error when mix phx.server

Compiling 2 files (.ex)
    warning: TrainWeb.Catalog.list_products/1 is undefined or private. Did you mean:

          * list_products/0

    │
 29 │     product = Catalog.list_products(id)
    │                       ~
    │
    └─ lib/train_web/controllers/product_controller.ex:29:23: TrainWeb.ProductController.show/2

I try to figure it out in list_products is same with example in guidelinse of Phoenix Framework

defmodule TrainWeb.ProductController do
	use TrainWeb, :controller

	alias TrainWeb.Catalog
	alias TrainWeb.Catalog.Product

	def index(conn, _params) do
		products = Catalog.list_products()
		render(conn, :index, products: products)
	end

	def new(conn, _params) do
		changeset = Catalog.change_product(%Product{})
		render(conn, :new, changeset: changeset)
	end

	def create(conn, %{"product" => product_params}) do
		case Catalog.create_product(product_params) do
			{:ok, product} ->
				conn
				|> put_flash(:info, "Product created successfully")
				|> redirect(to: ~p"/products/#{product}")
			{:error, %Ecto.Changeset{} = changeset} ->
				render(conn, :new, changeset: changeset)
		end
	end

  def show(conn, %{"id" => id}) do
    product = Catalog.list_products(id)
    render(conn, :show, product: product)
  end

	def edit(conn, %{"id" => id}) do
		product = Catalog.get_product!(id)
		changeset = Catalog.change_product(product)
		render(conn, :edit, product: product, changeset: changeset)
	end

	def update(conn, %{"id" => id, "product" => product_params}) do
		product = Catalog.get_product!(id)
		case Catalog.update_product(product, product_params) do
			{:ok, product} ->
				conn
				|> put_flash(:info, "Product updated successfully")
				|> redirect(to: ~p"/products/#{product}")
			{:error, %Ecto.Changeset{} = changeset} ->
				render(conn, :edit, product: product, changeset: changeset) 
		end
	end

	def delete(conn, %{"id" => id}) do
		product = Catalog.get_product!(id)
		{:ok, _product} = Catalog.delete_product(product)

		conn
		|> put_flash(:info, "Product deleted successfully")
		|> redirect(to: ~p"/products")
	end
end

The compiler is telling you that there is no function called Catalog.list_products that takes one argument.

In most Phoenix applications you will have a list_products function for a context, but it won’t take any arguments.

You probably want Catalog.get_product!(id)

1 Like

tks Bro @msmithstubbs i was miss copied from another code :smiley: