BadArityError says 2 arguments when IO.inspect shows 1

Hello all,

I’m getting BadArityError for Enum.reduce. It says “Function with arity 1 called with 2 arguments.”

I have a list of chats and when I run IO.inspect right before the Enum.reduce, here’s what I get:

[
  %Rocketship.Chats.Chat{
    __meta__: #Ecto.Schema.Metadata<:loaded, "chats">,
    chats_users: #Ecto.Association.NotLoaded<association :chats_users is not loaded>,
    cohort: #Ecto.Association.NotLoaded<association :cohort is not loaded>,
    cohort_id: nil,
    id: 159,
    inserted_at: ~N[2020-11-15 15:00:14],
    is_pinned: false,
    last_message: nil,
    name: "Awesome Team",
    organization: #Ecto.Association.NotLoaded<association :organization is not loaded>,
    organization_id: nil,
    parent_organization: #Ecto.Association.NotLoaded<association :parent_organization is not loaded>,
    parent_organization_id: nil,
    team: #Ecto.Association.NotLoaded<association :team is not loaded>,
    team_id: 2,
    type: "Team",
    updated_at: ~N[2020-11-15 15:00:14],
    users: #Ecto.Association.NotLoaded<association :users is not loaded>
  },
  %Rocketship.Chats.Chat{
    __meta__: #Ecto.Schema.Metadata<:loaded, "chats">,
    chats_users: #Ecto.Association.NotLoaded<association :chats_users is not loaded>,
    cohort: #Ecto.Association.NotLoaded<association :cohort is not loaded>,
    cohort_id: 2,
    id: 160,
    inserted_at: ~N[2020-11-15 15:00:14],
    is_pinned: false,
    last_message: nil,
    name: "Great Co + Awesome Team",
    organization: #Ecto.Association.NotLoaded<association :organization is not loaded>,
    organization_id: 2,
    parent_organization: #Ecto.Association.NotLoaded<association :parent_organization is not loaded>,
    parent_organization_id: 1,
    team: #Ecto.Association.NotLoaded<association :team is not loaded>,
    team_id: 2,
    type: "Cohort Team",
    updated_at: ~N[2020-11-15 15:00:14],
    users: #Ecto.Association.NotLoaded<association :users is not loaded>
  },
  %Rocketship.Chats.Chat{ 
    __meta__: #Ecto.Schema.Metadata<:loaded, "chats">,
    chats_users: #Ecto.Association.NotLoaded<association :chats_users is not loaded>,
    cohort: #Ecto.Association.NotLoaded<association :cohort is not loaded>,
    cohort_id: 2,
    id: 174,
    inserted_at: ~N[2020-11-15 15:00:14],
    is_pinned: false,
    last_message: nil,
    name: "Great Co + Cool Team",
    organization: #Ecto.Association.NotLoaded<association :organization is not loaded>,
    organization_id: 2,
    parent_organization: #Ecto.Association.NotLoaded<association :parent_organization is not loaded>,
    parent_organization_id: 1,
    team: #Ecto.Association.NotLoaded<association :team is not loaded>,
    team_id: 22,
    type: "Cohort Team",
    updated_at: ~N[2020-11-15 15:00:14],
    users: #Ecto.Association.NotLoaded<association :users is not loaded>
  }
]

It looks like a normal list, which would be one argument. Can anyone see what I’m missing?

Here’s the full error:

** (BadArityError) #Function<5.124441222/1 in Rocketship.Chats.create_team_chats/1>
with arity 1 called with 2 arguments (%Rocketship.Chats.Chat{__meta__: #Ecto.Schema.Metadata<:loaded, "chats">, chats_users: 
#Ecto.Association.NotLoaded<association :chats_users is not loaded>, cohort:
#Ecto.Association.NotLoaded<association :cohort is not loaded>, cohort_id: nil, id: 159,
inserted_at: ~N[2020-11-15 15:00:14], is_pinned: false, last_message: nil, name: "Awesome Team",
organization: #Ecto.Association.NotLoaded<association :organization is not loaded>,
organization_id: nil, parent_organization: #Ecto.Association.NotLoaded<association
:parent_organization is not loaded>, parent_organization_id: nil, team:
#Ecto.Association.NotLoaded<association :team is not loaded>, team_id: 2, type: "Team",
updated_at: ~N[2020-11-15 15:00:14], users: #Ecto.Association.NotLoaded<association :users
is not loaded>}, [])
    (elixir 1.10.4) lib/enum.ex:2111: Enum."-reduce/3-lists^foldl/2-0-"/3
    (rocketship 0.1.0) lib/rocketship/chats.ex:368: anonymous fn/3 in Rocketship.Chats.create_team_chats/1
    (ecto 3.5.4) lib/ecto/multi.ex:694: anonymous fn/5 in Ecto.Multi.operation_fun/2
    (ecto 3.5.4) lib/ecto/multi.ex:646: Ecto.Multi.apply_operation/5
    (elixir 1.10.4) lib/enum.ex:2111: Enum."-reduce/3-lists^foldl/2-0-"/3
    (ecto 3.5.4) lib/ecto/multi.ex:630: anonymous fn/5 in Ecto.Multi.apply_operations/5
    (ecto_sql 3.5.3) lib/ecto/adapters/sql.ex:1027: anonymous fn/3 in Ecto.Adapters.SQL.checkout_or_transaction/4
    (db_connection 2.3.0) lib/db_connection.ex:1426: DBConnection.run_transaction/4
    (ecto 3.5.4) lib/ecto/repo/transaction.ex:20: Ecto.Repo.Transaction.transaction/4

Here’s the call to Enum.reduce:

Enum.reduce(chats, [], fn chat ->

The anonymous function inside Enum.reduce is given two arguments. The first is one of the items chat which you have, but the second arg is the accumulator. You need Enum.reduce(chats, [], fn chat, list ->.

This does actually show in the error, here is the error again with some of the inner noise removed:

** (BadArityError) #Function<5.124441222/1 in Rocketship.Chats.create_team_chats/1>
with arity 1 called with 2 arguments (%Rocketship.Chats.Chat{...}, [])
3 Likes