简体   繁体   中英

(FunctionClauseError) no function clause matching in CheckersgameWeb.GamesChannel.handle_in/3

I'm currently building a checkers game in phoenix that's hitting into a "FunctionClauseError". The error is being thrown by my "handle_in" function in the games channel which I've provided here:

def handle_in("click", %{"click" => ll}, socket) do
    IO.puts("whatever")
end

Any ideas as to what we're doing wrong?

Here's an example:

defmodule Example do
  def run() do
    Demo.handle_in("UNEXPECTED", "foo", "bar")
  end

  def handle_in("click", map, socket) do
    IO.puts("whatever")
  end
end

And here's the error message when we call run/0 :

iex(1)> Example.run
** (FunctionClauseError) no function clause matching in Example.handle_in/3

    The following arguments were given to Example.handle_in/3:

        # 1
        "UNEXPECTED"

        # 2
        "foo"

        # 3
        "bar"

You can see from this output, that run/1 calls handle_in/3 with "UNEXPECTED" as its first argument. There is no clause of handle_in that expects that, so elixir generates the error. A common way to handle this if you can't control the inputs is to add a catch-all clause that does not pattern-match the arguments:

def handle_in("click", map, socket) do
  IO.puts("clicked")
end

def handle_in(one, two, three) do
  IO.puts("Called with: #{inspect one}, #{inspect two}, #{inspect three}")
end

Now the error is not produced, because the second clause can handle the "UNEXPECTED" string:

iex(1)> Example.run
Called with: "UNEXPECTED", "foo", "bar"
:ok

In your case, it could be that the "click" string is not passed, or that the 2nd argument isn't a map that contains the "click" key, but without the calling code or error message, it's impossible to tell.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM