How to properly inspect contents of a context function. Why does my empty piped in function create an error?

I just want to inspect the innards of a function. From my perspective I am simply piping data in and out without affecting it. Why does this create an error at all?

function App.Alerts.Alert.check_for_single_selection/1 is undefined or private

  def check_for_single_selection(data)do 
      IO.inspect "view something"
      data
  end


  def create_alert(attrs \\ %{}) do
    %Alert{}
    |> Alert.check_for_single_selection()
    |> Alert.changeset(attrs)
    |> Repo.insert()
  end

The error is saying that there is no App.Alerts.Alert.check_for_single_selection/1 function. You’re showing a check_for_single_selection/1 function, what module is it in?

1 Like

I see.
I fixed it like this:

|> App.Alerts.check_for_single_selection()

For some reason I thought I could omit the App from the naming.

The full code is below



defmodule App.Alerts do
  @moduledoc """
  The Alerts context.
  """

  import Ecto.Query, warn: false
  alias App.Repo

  alias App.Alerts.Alert

  @doc """
  Returns the list of alerts.

  ## Examples

      iex> list_alerts()
      [%Alert{}, ...]

  """

  def list_alerts do
    Repo.all(Alert)
  end

  @doc """
  Gets a single alert.

  Raises `Ecto.NoResultsError` if the Alert does not exist.

  ## Examples

      iex> get_alert!(123)
      %Alert{}

      iex> get_alert!(456)
      ** (Ecto.NoResultsError)

  """
  def get_alert!(id), do: Repo.get!(Alert, id)

  @doc """
  Creates a alert.

  ## Examples

      iex> create_alert(%{field: value})
      {:ok, %Alert{}}

      iex> create_alert(%{field: bad_value})
      {:error, %Ecto.Changeset{}}

  """



  def check_for_single_selection(data) do 
      IO.inspect "view something"
      data
  end


  def create_alert(attrs \\ %{}) do
    %Alert{}
    |> App.Alerts.check_for_single_selection()
    |> Alert.changeset(attrs)
    |> Repo.insert()
  end

  @doc """
  Updates a alert.

  ## Examples

      iex> update_alert(alert, %{field: new_value})
      {:ok, %Alert{}}

      iex> update_alert(alert, %{field: bad_value})
      {:error, %Ecto.Changeset{}}

  """
  def update_alert(%Alert{} = alert, attrs) do
    alert
    |> Alert.changeset(attrs)
    |> Repo.update()
  end

  @doc """
  Deletes a alert.

  ## Examples

      iex> delete_alert(alert)
      {:ok, %Alert{}}

      iex> delete_alert(alert)
      {:error, %Ecto.Changeset{}}

  """
  def delete_alert(%Alert{} = alert) do
    Repo.delete(alert)
  end

  @doc """
  Returns an `%Ecto.Changeset{}` for tracking alert changes.

  ## Examples

      iex> change_alert(alert)
      %Ecto.Changeset{data: %Alert{}}

  """
  def change_alert(%Alert{} = alert, attrs \\ %{}) do
    Alert.changeset(alert, attrs)
  end





end

Hey @wktdev in your first function the module you used was App.Alerts.Alert, but the function wasn’t in that module at all, and that doesn’t really have anything to do with leaving off the App bit.