(FunctionClauseError) no function clause matching in Access.get/3

落花浮王杯 提交于 2019-12-13 00:27:23

问题


I am new to Elixir and currently making a transaction from Ruby. I am struggling to get around an error I am getting and because Elixir does not have object.class, I am struggling to understand what data type am I returning back and how to troubleshoot it.

Anyway I am trying to seed a database from a CSV but getting the error

Below is my code

File.stream!('users_departs.csv')
|> Stream.drop(1)
|> CSV.decode(headers: [:name, :title, :departments])
|> Enum.take(10
|> Enum.each( fn(x) -> IO.inspect(x[:ok]) end )

 Error

** (FunctionClauseError) no function clause matching in Access.get/3

The following arguments were given to Access.get/3:

    # 1
    {:ok,
     %{
       departments: "Sales|Marketing",
       name: "John Smith",
       title: "Customer Service"
     }}

    # 2
    :ok

    # 3
    nil
(elixir) lib/access.ex:306: Access.get/3
(stdlib) erl_eval.erl:670: :erl_eval.do_apply/6
(stdlib) erl_eval.erl:878: :erl_eval.expr_list/6

I have 2 models, User, and Departments and I would like to seed the departments first, then create a user and then associate a user-department mapping but am struggling to get past this step.

Any help will be appreciated


回答1:


Access.get/3 is a elixir function, that, according to the documentation:

Gets the value for the given key in a container (a map, keyword list, or struct that implements the Access behaviour)

Check the error msg,

{:ok,
   %{
     departments: "Sales|Marketing",
     name: "John Smith",
     title: "Customer Service"}}

is not “map, keyword list or struct etc.” It is a tuple, consisting of :ok atom and the map.

That seems, CSV.decode(headers: [:name, :title, :departments]) returns {:ok, value_decode}.

So you can not pipe it to Enum.take/2 as is.

Just

{:ok, decode_csv} =
  File.stream!('users_departs.csv')
  |> Stream.drop(1)
  |> CSV.decode(headers: [:name, :title, :departments])

decode_csv
|> Enum.take(10)
|> Enum.each( fn(x) -> IO.inspect(x[:ok]) end )


来源:https://stackoverflow.com/questions/50671952/functionclauseerror-no-function-clause-matching-in-access-get-3

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!