Protocol Enumerable not implemented for %SmartcomClient.Messages.Message{__meta__:

I don’t understand this error I am trying to display data from the database,
i am getting the error below.

protocol Enumerable not implemented for %SmartcomClient.Messages.Message{__meta:

#Ecto.Schema.Metadata<:loaded, “messages”>, date_sent: “Dec 29 2020 1:52PM”, id: 5, inserted_at: ~N[2020-12-29 13:52:22], message: “TEST”, phone: “26095424399”, short_code: “2607000”, status: “unread”, updated_at: ~N[2020-12-29 13:52:22]} of type SmartcomClient.Messages.Message (a struct)

hello, I’m sure you’d get detailed answers if you could show the code producing the error :grin:

its just the default elixir data display code

<%= for message <- @messages do %>

<tr>
  <td><%=message.id %></td>
  <td><%= message.date_sent %></td>
  <td><%= message.message %></td>
  <td><%= message.short_code %></td>
  <td><%= message.status %></td>
  <td><%= message.phone %></td>
  <td>
    <span><%= link "Show", to: Routes.message_path(@conn, :show, message) %></span>
    <span><%= link "Edit", to: Routes.message_path(@conn, :edit, message) %></span>
    <span><%= link "Delete", to: Routes.message_path(@conn, :delete, message), method: :delete, data: [confirm: "Are you sure?"] %></span>
  </td>
</tr>

<% end %>

Date sent Message Short code Status Phone

<%= link “New Message”, to: Routes.message_path(@conn, :new) %>

it seems that the assign @messages is not an (enumerable) list but a single SmartcomClient.Messages.Message (a struct)

so i have to change my struct to a list?

I’d check in the controller and the context module how the messages are collected, and make sure @messages is a list of zero or more Message structs.

def list_messages do
Repo.all(Message)
end

Repo.all() returns a list (of schema structs), so if @messages gets its value from list_messages I’d expect it to be a list. How the assign is defined in the controller?

One way I’ve seen this kind of error happen is with path helpers - if the route isn’t expecting a value, Routes.message_path(@conn, :edit, message) (for example only, not sure if it’s the problem) will raise exactly that exception because it tries to treat the struct in message as an option list…

1 Like