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
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 )
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.