# Crash when after using the delete\_entry function

**URL:** <https://forum.elixirforum.com/t/crash-when-after-using-the-delete-entry-function/31691>\
**Category:** Questions\
**Tags:** learning-elixir\
**Created:** [May 21, 2020, 10:36am UTC](https://forum.elixirforum.com/t/crash-when-after-using-the-delete-entry-function/31691 "2020-05-21T10:36:41Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![chan11347](https://forum.elixirforum.com/letter_avatar_proxy/v4/letter/c/e0b2c6/32.png) [@chan11347](https://forum.elixirforum.com/u/chan11347)\
**Post date:** [May 21, 2020, 10:36am UTC](https://forum.elixirforum.com/t/crash-when-after-using-the-delete-entry-function/31691/1 "2020-05-21T10:36:41Z")

</div>

```elixir
iex(1)> {:ok, todo_system_pid} = Todo.System.start_link()
Starting database worker 1
Starting database worker 2
Starting database worker 3
Starting to-do cache
{:ok, #PID<0.141.0>}
iex(2)> {:ok, todo_system_pid: todo_system_pid}
{:ok, [todo_system_pid: #PID<0.141.0>]}
iex(3)> abc = Todo.Cache.server_process("abc")
Starting to-do server for abc
#PID<0.151.0>
iex(4)> Todo.Server.add_entry(abc, %{date: ~D[2020-05-19], time: ~T[00:00:00], title: "Shopping"})
:ok
iex(5)> Todo.Server.add_entry(abc, %{date: ~D[2020-05-19], time: ~T[00:01:00], title: "Hacking"})
:ok
iex(6)> Todo.Server.add_entry(abc, %{date: ~D[2020-05-20], time: ~T[00:00:00], title: "Swimming"})
:ok
iex(7)> Todo.Server.entries(abc, ~D[2020-05-19], ~T[00:00:00])
[%{date: ~D[2020-05-19], id: 22, time: ~T[00:00:00], title: "Shopping"}]
iex(8)> Todo.Server.date_check(abc, ~D[2020-05-20])
[
  %{date: ~D[2020-05-20], id: 1, time: ~T[16:52:00], title: "Shopping"},
  %{date: ~D[2020-05-20], id: 2, time: ~T[16:53:00], title: "Playing"},
  %{date: ~D[2020-05-20], id: 3, time: ~T[16:53:00], title: "swimming"},
  %{date: ~D[2020-05-20], id: 4, time: ~T[16:52:00], title: "Shopping"},
  %{date: ~D[2020-05-20], id: 5, time: ~T[16:53:00], title: "Playing"},
  %{date: ~D[2020-05-20], id: 6, time: ~T[16:53:00], title: "swimming"},
  %{date: ~D[2020-05-20], id: 7, time: ~T[16:52:00], title: "Shopping"},
  %{date: ~D[2020-05-20], id: 8, time: ~T[16:53:00], title: "Playing"},
  %{date: ~D[2020-05-20], id: 9, time: ~T[16:52:00], title: "Shopping"},
  %{date: ~D[2020-05-20], id: 10, time: ~T[16:53:00], title: "Playing"},
  %{date: ~D[2020-05-20], id: 11, time: ~T[16:53:00], title: "swimming"},
  %{date: ~D[2020-05-20], id: 12, time: ~T[16:52:00], title: "Shopping"},
  %{date: ~D[2020-05-20], id: 13, time: ~T[16:53:00], title: "Playing"},
  %{date: ~D[2020-05-20], id: 14, time: ~T[16:53:00], title: "swimming"},
  %{date: ~D[2020-05-20], id: 15, time: ~T[16:53:00], title: "Playing"},
  %{date: ~D[2020-05-20], id: 16, time: ~T[16:52:00], title: "Shopping"},
  %{date: ~D[2020-05-20], id: 17, time: ~T[16:53:00], title: "Playing"},
  %{date: ~D[2020-05-20], id: 18, time: ~T[16:53:00], title: "swimming"},
  %{date: ~D[2020-05-20], id: 19, time: ~T[16:52:00], title: "Shopping"},
  %{date: ~D[2020-05-20], id: 20, time: ~T[16:53:00], title: "Playing"},
  %{date: ~D[2020-05-20], id: 21, time: ~T[16:53:00], title: "swimming"},
  %{date: ~D[2020-05-20], id: 24, time: ~T[00:00:00], title: "Swimming"}
]
iex(9)> Todo.Server.time_check(abc, ~T[00:00:00])
[
  %{date: ~D[2020-05-19], id: 22, time: ~T[00:00:00], title: "Shopping"},
  %{date: ~D[2020-05-20], id: 24, time: ~T[00:00:00], title: "Swimming"}
]
iex(10)> Todo.Server.delete_entry(abc, 24)
:ok
iex(11)> Todo.Server.time_check(abc, ~T[00:00:00])

18:32:46.287 [error] GenServer {Todo.ProcessRegistry, {Todo.Server, "abc"}} terminating
** (ArgumentError) argument error
    :erlang.apply("abc", :entries, [])
    (todo) lib/todo/list.ex:30: Todo.List.time_check/2
    (todo) lib/todo/server.ex:58: Todo.Server.handle_call/3
    (stdlib) gen_server.erl:661: :gen_server.try_handle_call/4
    (stdlib) gen_server.erl:690: :gen_server.handle_msg/6
    (stdlib) proc_lib.erl:249: :proc_lib.init_p_do_apply/3
Last message (from #PID<0.139.0>): {:time_check, ~T[00:00:00]}
State: {%Todo.List{auto_id: 25, entries: %{1 => %{date: ~D[2020-05-20], id: 1, time: ~T[16:52:00], title: "Shopping"}, 2 => %{date: ~D[2020-05-20], id: 2, time: ~T[16:53:00], title: "Playing"}, 3 => %{date: ~D[2020-05-20], id: 3, time: ~T[16:53:00], title: "swimming"}, 4 => %{date: ~D[2020-05-20], id: 4, time: ~T[16:52:00], title: "Shopping"}, 5 => %{date: ~D[2020-05-20], id: 5, time: ~T[16:53:00], title: "Playing"}, 6 => %{date: ~D[2020-05-20], id: 6, time: ~T[16:53:00], title: "swimming"}, 7 => %{date: ~D[2020-05-20], id: 7, time: ~T[16:52:00], title: "Shopping"}, 8 => %{date: ~D[2020-05-20], id: 8, time: ~T[16:53:00], title: "Playing"}, 9 => %{date: ~D[2020-05-20], id: 9, time: ~T[16:52:00], title: "Shopping"}, 10 => %{date: ~D[2020-05-20], id: 10, time: ~T[16:53:00], title: "Playing"}, 11 => %{date: ~D[2020-05-20], id: 11, time: ~T[16:53:00], title: "swimming"}, 12 => %{date: ~D[2020-05-20], id: 12, time: ~T[16:52:00], title: "Shopping"}, 13 => %{date: ~D[2020-05-20], id: 13, time: ~T[16:53:00], title: "Playing"}, 14 => %{date: ~D[2020-05-20], id: 14, time: ~T[16:53:00], title: "swimming"}, 15 => %{date: ~D[2020-05-20], id: 15, time: ~T[16:53:00], title: "Playing"}, 16 => %{date: ~D[2020-05-20], id: 16, time: ~T[16:52:00], title: "Shopping"}, 17 => %{date: ~D[2020-05-20], id: 17, time: ~T[16:53:00], title: "Playing"}, 18 => %{date: ~D[2020-05-20], id: 18, time: ~T[16:53:00], title: "swimming"}, 19 => %{date: ~D[2020-05-20], id: 19, time: ~T[16:52:00], title: "Shopping"}, 20 => %{date: ~D[2020-05-20], id: 20, time: ~T[16:53:00], title: "Playing"}, 21 => %{date: ~D[2020-05-20], id: 21, time: ~T[16:53:00], title: "swimming"}, 22 => %{date: ~D[2020-05-19], id: 22, time: ~T[00:00:00], title: "Shopping"}, 23 => %{date: ~D[2020-05-19], id: 23, time: ~T[00:01:00], title: "Hacking"}}}, "abc"}
Client #PID<0.139.0> is alive

    (stdlib) gen.erl:169: :gen.do_call/4
    (elixir) lib/gen_server.ex:1006: GenServer.call/3
    (stdlib) erl_eval.erl:680: :erl_eval.do_apply/6
    (elixir) src/elixir.erl:275: :elixir.eval_forms/4
    (iex) lib/iex/evaluator.ex:257: IEx.Evaluator.handle_eval/5
    (iex) lib/iex/evaluator.ex:237: IEx.Evaluator.do_eval/3
    (iex) lib/iex/evaluator.ex:215: IEx.Evaluator.eval/3
    (iex) lib/iex/evaluator.ex:103: IEx.Evaluator.loop/1
** (exit) exited in: GenServer.call(#PID<0.151.0>, {:time_check, ~T[00:00:00]}, 5000)
    ** (EXIT) an exception was raised:
        ** (ArgumentError) argument error
            :erlang.apply("abc", :entries, [])
            (todo) lib/todo/list.ex:30: Todo.List.time_check/2
            (todo) lib/todo/server.ex:58: Todo.Server.handle_call/3
            (stdlib) gen_server.erl:661: :gen_server.try_handle_call/4
            (stdlib) gen_server.erl:690: :gen_server.handle_msg/6
            (stdlib) proc_lib.erl:249: :proc_lib.init_p_do_apply/3
    (elixir) lib/gen_server.ex:1009: GenServer.call/3
iex(11)>

```

so this is what i do, i was trying to use this function to delete the entry but after i check my list again, there is an error comming out, this is my code for delete\_entry  
list.ex

```elixir
def delete_entry(todo_list, entry_id) do	  
          new_entries = Map.delete(todo_list.entries, entry_id)
          %Todo.List{todo_list | entries: new_entries}
	end

```

server.ex

```elixir
def delete_entry(todo_server, entry_id) do
    GenServer.cast(todo_server, {:delete_entry, entry_id})
  end

@impl GenServer
  def handle_cast({:delete_entry, entry_id}, {name, todo_list}) do
    {:noreply, {Todo.List.delete_entry(todo_list, entry_id), name}}
  end

```

---

<div class="post-metadata">

**Author:** ![kokolegorille](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/kokolegorille/32/4784_2.png) [@kokolegorille](https://forum.elixirforum.com/u/kokolegorille)\
**Post date:** [May 21, 2020, 10:58am UTC](https://forum.elixirforum.com/t/crash-when-after-using-the-delete-entry-function/31691/2 "2020-05-21T10:58:32Z")

</div>

> [@chan11347](#):
>
> ```elixir
> def handle_cast({:delete_entry, entry_id}, {name, todo_list}) do
> {:noreply, {Todo.List.delete_entry(todo_list, entry_id), name}}
> end
> 
> ```

Here You are switching position in the tuple…

> [@chan11347](#):
>
> `(todo) lib/todo/list.ex:30: Todo.List.time_check/2`

That is where You should find the error.

---

<div class="post-metadata">

**Author:** ![chan11347](https://forum.elixirforum.com/letter_avatar_proxy/v4/letter/c/e0b2c6/32.png) [@chan11347](https://forum.elixirforum.com/u/chan11347)\
**Post date:** [May 21, 2020, 12:02pm UTC](https://forum.elixirforum.com/t/crash-when-after-using-the-delete-entry-function/31691/3 "2020-05-21T12:02:36Z")

</div>

sorry but if i dont understand what switching position in the tuple meaning are you saying

```elixir
def handle_cast({:delete_entry, entry_id}, {name, todo_list}) do
    {:noreply, {Todo.List.delete_entry(todo_list, entry_id), name}}
  end

```

changed to

```elixir
def handle_cast({:delete_entry, entry_id}, {todo_list,name}) do
    {:noreply, {Todo.List.delete_entry(todo_list, entry_id), name}}
  end

```

?

---

<div class="post-metadata">

**Author:** ![kokolegorille](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/kokolegorille/32/4784_2.png) [@kokolegorille](https://forum.elixirforum.com/u/kokolegorille)\
**Post date:** [May 21, 2020, 12:05pm UTC](https://forum.elixirforum.com/t/crash-when-after-using-the-delete-entry-function/31691/4 "2020-05-21T12:05:12Z")

</div>

The state of your genserver is a tuple…

Here You put

```elixir
{name, todo_list}

```

But You return…

```elixir
{Todo.List.delete_entry(todo_list, entry_id), name}

```

You are switching the information in the tuple, maybe this is what You want?

What is the initial state of the Genserver?

---

<div class="post-metadata">

**Author:** ![chan11347](https://forum.elixirforum.com/letter_avatar_proxy/v4/letter/c/e0b2c6/32.png) [@chan11347](https://forum.elixirforum.com/u/chan11347)\
**Post date:** [May 21, 2020, 12:14pm UTC](https://forum.elixirforum.com/t/crash-when-after-using-the-delete-entry-function/31691/5 "2020-05-21T12:14:25Z")

</div>

i want to remove the information in the list

```elixir
Todo.Server.date_check(abc, ~D[2020-05-20])
[
  %{date: ~D[2020-05-20], **id: 1** , time: ~T[16:52:00], title: "Shopping"},
  %{date: ~D[2020-05-20], id: 2, time: ~T[16:53:00], title: "Playing"},
  %{date: ~D[2020-05-20], id: 3, time: ~T[16:53:00], title: "swimming"},
]

```

which i try to delete it by the id

if you talk about the init

```elixir
@impl GenServer
  def init(name) do
    IO.puts("Starting to-do server for #{name}")
    {:ok, {name, Todo.Database.get(name) || Todo.List.new()}}
  end

```

abc = Todo.Cache.server\_process(“abc”)  
Starting to-do server for abc  
#pid\<0.151.0\>

---

<div class="post-metadata">

**Author:** ![kokolegorille](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/kokolegorille/32/4784_2.png) [@kokolegorille](https://forum.elixirforum.com/u/kokolegorille)\
**Post date:** [May 21, 2020, 12:22pm UTC](https://forum.elixirforum.com/t/crash-when-after-using-the-delete-entry-function/31691/6 "2020-05-21T12:22:16Z")

</div>

> [@chan11347](#):
>
> (todo) lib/todo/list.ex:30: Todo.List.time\_check/2

The error is in this file, at line 30…

And You should be careful what You return, because You switch the list and the name in the tuple.

> [@chan11347](#):
>
> `{:noreply, {Todo.List.delete_entry(todo_list, entry_id), name}}`

---

<div class="post-metadata">

**Author:** ![chan11347](https://forum.elixirforum.com/letter_avatar_proxy/v4/letter/c/e0b2c6/32.png) [@chan11347](https://forum.elixirforum.com/u/chan11347)\
**Post date:** [May 21, 2020, 12:23pm UTC](https://forum.elixirforum.com/t/crash-when-after-using-the-delete-entry-function/31691/7 "2020-05-21T12:23:33Z")

</div>

```elixir
29 def time_check(todo_list, time) do
30 todo_list.entries
31 |> Stream.filter(fn {_, entry} -> entry.time == time end)
32 |> Enum.map(fn {_, entry} -> entry end)
33 end

```

i did try change it to todo\_list.time\_check  
but it just crash it at the first time

and if i change to this

```elixir
def delete_entry(todo_list, entry_id) do
    %Todo.List{todo_list | entries: Map.delete(todo_list.entries, entry_id)}
  end

```

will it better? since i try to copy the add\_entry format to do it

```elixir
def add_entry(todo_list, entry) do
    entry = Map.put(entry, :id, todo_list.auto_id)
    new_entries = Map.put(todo_list.entries, todo_list.auto_id, entry)

    %Todo.List{todo_list | entries: new_entries, auto_id: todo_list.auto_id + 1}
  end

```

---

<div class="post-metadata">

**Author:** ![kokolegorille](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/kokolegorille/32/4784_2.png) [@kokolegorille](https://forum.elixirforum.com/u/kokolegorille)\
**Post date:** [May 21, 2020, 12:28pm UTC](https://forum.elixirforum.com/t/crash-when-after-using-the-delete-entry-function/31691/8 "2020-05-21T12:28:32Z")

</div>

> [@chan11347](#):
>
> ```elixir
> 29 def time_check(todo_list, time) do
> 30 todo_list.entries
> 31 |> Stream.filter(fn {_, entry} -> entry.time == time end)
> 32 |> Enum.map(fn {_, entry} -> entry end)
> 33 end
> 
> ```

Try to inspect the todo\_list. My guess is You don’t provide the right parameter to the function.

---

<div class="post-metadata">

**Author:** ![chan11347](https://forum.elixirforum.com/letter_avatar_proxy/v4/letter/c/e0b2c6/32.png) [@chan11347](https://forum.elixirforum.com/u/chan11347)\
**Post date:** [May 21, 2020, 12:41pm UTC](https://forum.elixirforum.com/t/crash-when-after-using-the-delete-entry-function/31691/9 "2020-05-21T12:41:22Z")

</div>

Let say i step by step

```elixir
1.{:ok, todo_system_pid} = Todo.System.start_link()
2.{:ok, todo_system_pid: todo_system_pid}
3.a = Todo.Cache.server_process("a")
4.Todo.Server.add_entry(a, %{date: ~D[2020-05-19], time: ~T[16:52:00], title: "Shopping"})

```

above all return :ok  
`Todo.Server.entries(a, ~D[2020-05-19], ~T[16:52:00])`

> [%{date: ~D[2020-05-19], id: 1, time: ~T[16:52:00], title: “Shopping”}]  
> `Todo.Server.date_check(a, ~D[2020-05-19])`  
> [%{date: ~D[2020-05-19], id: 1, time: ~T[16:52:00], title: “Shopping”}]  
> `Todo.Server.delete_entry(a, 1)`  
> :ok  
> `Todo.Server.entries(a, ~D[2020-05-19], ~T[16:52:00])`  
> crashed

---

<div class="post-metadata">

**Author:** ![kokolegorille](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/kokolegorille/32/4784_2.png) [@kokolegorille](https://forum.elixirforum.com/u/kokolegorille)\
**Post date:** [May 21, 2020, 12:48pm UTC](https://forum.elixirforum.com/t/crash-when-after-using-the-delete-entry-function/31691/10 "2020-05-21T12:48:28Z")

</div>

Let say You correct this wrong code…

```elixir
def handle_cast({:delete_entry, entry_id}, {name, todo_list}) do
    {:noreply, {Todo.List.delete_entry(todo_list, entry_id), name}}
end

# Wrong! it should be

def handle_cast({:delete_entry, entry_id}, {name, todo_list}) do
    {:noreply, {name, Todo.List.delete_entry(todo_list, entry_id)}}
end

```

Let say You put an inspect, and tell us what You got…

```elixir
29 def time_check(todo_list, time) do
--> IO.inspect todo_list, label: "HEY, WHAZZUP"
30 todo_list.entries
31 |> Stream.filter(fn {_, entry} -> entry.time == time end)
32 |> Enum.map(fn {_, entry} -> entry end)
33 end

```

---

<div class="post-metadata">

**Author:** ![chan11347](https://forum.elixirforum.com/letter_avatar_proxy/v4/letter/c/e0b2c6/32.png) [@chan11347](https://forum.elixirforum.com/u/chan11347)\
**Post date:** [May 21, 2020, 12:58pm UTC](https://forum.elixirforum.com/t/crash-when-after-using-the-delete-entry-function/31691/11 "2020-05-21T12:58:40Z")

</div>

So it isn’t the input value is on the part of the Todo.List.delete\_entry(todo\_list, entry\_id) ?  
and why when the name should put it into the front make a big difference?

---

<div class="post-metadata">

**Author:** ![kokolegorille](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/kokolegorille/32/4784_2.png) [@kokolegorille](https://forum.elixirforum.com/u/kokolegorille)\
**Post date:** [May 21, 2020, 1:07pm UTC](https://forum.elixirforum.com/t/crash-when-after-using-the-delete-entry-function/31691/12 "2020-05-21T13:07:23Z")

</div>

It is not important if You put the name first or last. but You need to keep it consistant.

{a, b} is not equal to {b, a}

unless a == b of course.
