** (UndefinedFunctionError) function BookList.UserSpace.Invitation.fetch/2
is undefined (BookList.UserSpace.Invitation does not implement the Access behaviour)
I must be missing use or alias or something. The error comes after the line IO.inspect below.
defmodule BookListWeb.InvitationView do
use BookListWeb, :view
alias BookListWeb.InvitationView
def render("index.json", %{invitations: invitations}) do
IO.inspect invitations, label: "INV (XX)"
%{data: render_many(invitations, InvitationView, "invitation.json")}
end
....
This error happens when you try to do x[:y] where x is a struct, e.g.:
iex> Version.parse!("1.0.0")[:major]
** (UndefinedFunctionError) function Version.fetch/2 is undefined (Version does not implement the Access behaviour)
(elixir) Version.fetch(#Version<1.0.0>, :major)
(elixir) lib/access.ex:267: Access.get/3
defmodule BookListWeb.InvitationController do
use BookListWeb, :controller
alias BookList.UserSpace
alias BookList.Repo
alias BookList.UserSpace.Invitation
action_fallback BookListWeb.FallbackController
def index(conn, params) do
invitations = Repo.all(Invitation)
render(conn, "index.json", invitations: invitations)
end
end
I added the controller function below to BookListWeb.InvitationController. It works as expected.
def invite(conn, invitation) do
IO.inspect invitation, label: "INVITATION"
cs = Invitation.changeset(%Invitation{}, invitation)
case cs.valid? do
true ->
Repo.insert(cs)
render(conn, "invitation.json", %{invitation: invitation})
false ->
render(conn, "error.json", %{error: "Could not make invitation"})
end
end
function BookList.UserSpace.Invitation.fetch/2 is undefined (BookList.UserSpace.Invitation does not implement the Access behaviour)
It refers to BookList.UserSpace.Invitation —don’t see what’s wrong, but the problem must be here.
defmodule BookList.UserSpace.Invitation do
use Ecto.Schema
import Ecto.Changeset
import Ecto.Query
alias BookList.Repo
alias BookList.UserSpace.Invitation
schema "invitations" do
field :invitee, :string
field :inviter, :string
field :group_name, :string
field :group_id, :integer
field :status, :string
timestamps()
end
def changeset(struct, params \\ %{}) do
struct
|> cast(params, [:invitee, :inviter, :group_name, :group_id, :status ])
|> validate_required([:invitee, :inviter])
end
end
that’s logger output, could you paste the full stacktrace from the exception? My guess is, the exception happened in a template, the controller code looks ok, and if so please attach that too.