(FunctionClauseError)Access.get / 3中没有匹配的功能子句 [英] (FunctionClauseError) no function clause matching in Access.get/3
问题描述
我不熟悉Elixir,目前正在与Ruby进行交易。我正在努力解决我遇到的错误,并且由于Elixir没有 object.class
,我正在努力了解返回的数据类型以及如何进行故障排除它。
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.
无论如何,我试图从CSV播种数据库但出现错误
Anyway I am trying to seed a database from a CSV but getting the error
下面是我的代码
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
我有2个模型:用户和部门,我想先给部门播种,然后创建一个用户,然后关联一个用户部门映射,但是
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.
任何帮助将不胜感激
推荐答案
Access.get / 3
是一种长生不老药功能,根据文档所述:
Access.get/3
is a elixir function, that, according to the documentation:
获取容器中给定键的值(实现
Access
行为的映射,关键字列表或结构)
Gets the value for the given key in a container (a map, keyword list, or struct that implements the
Access
behaviour)
检查错误消息,
{:ok,
%{
departments: "Sales|Marketing",
name: "John Smith",
title: "Customer Service"}}
不是地图,关键字列表或结构等。它是一个 tuple ,由:ok
原子和地图。
is not "map, keyword list or struct etc." It is a tuple, consisting of :ok
atom and the map.
似乎是 CSV.decode(标题: [:name,:title,:departments])
返回 {:ok,value_decode}
。
因此您无法将其通过管道传输到 Enum.take / 2
。
So you can not pipe it to Enum.take/2
as is.
只是
{: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 )
这篇关于(FunctionClauseError)Access.get / 3中没有匹配的功能子句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!