简体   繁体   English

这个Elixir讯息是什么意思

[英]What is this Elixir message means

I am writing Elixir to get record from remote nodes, I have write a module, 我正在编写Elixir以从远程节点获取记录,我已经编写了一个模块,

   defmodule Connect do
      def connect do
      node_ap_dev_ejd = :'abc@abc.com'
      :net_adm.ping(node_ap)

      fbUsersFun = fn(x) -> :binary.part(x,{0,3}) == <<"*ab">> end
      f = fn()-> :mnesia.dirty_select(:'cz_map',[{{:cz_map, :'$1', :'_',:'_',:'_'},[],[:'$1']}]) end

      fbUserList = :rpc.call(node_ap_dev_ejd,:mnesia,:activity,[:async_dirty,f])
      list = Enum.filter(fbUserList ,fbUsersFun)
      length(list)
      end
   end    

I can run the code if I put it in iex shell line by line, however if I compile the code and run Connect.connect , this error appear, I have no idea of it, please suggest 如果将代码逐行放置在iex shell中,则可以运行代码,但是如果编译代码并运行Connect.connect,则会出现此错误,我不知道它,请建议

** (Protocol.UndefinedError) protocol Enumerable not implemented for 
{:badrpc, {:EXIT, {:undef, [{#Function<1.96315226/0 in Connect.connect/0>, [], []}, {:mnesia_tm, :non_transaction, 5, [file: 'mnesia_tm.erl', line: 738]}]}}}
(elixir) lib/enum.ex:1: Enumerable.impl_for!/1
(elixir) lib/enum.ex:112: Enumerable.reduce/3
(elixir) lib/enum.ex:666: Enum.filter/2
         second_function.ex:10: Connect.connect/0

It means that the Enumerable protocol is not implemented for the data {:badrpc, ...} . 这意味着未为数据{:badrpc, ...}实现Enumerable协议

Most likely, that error comes from this line: 该错误最有可能来自以下行:

list = Enum.filter(fbUserList ,fbUsersFun)

In that line, you're trying to filter fbUserList which I guess is {:badrpc, ...} instead of an enumerable. 在该行中,您尝试过滤fbUserList ,我猜它是{:badrpc, ...}而不是可枚举的。 Tuples are not enumerables; 元组不是不可枚举的; lists and maps (and other things) are. 列表和地图(以及其他内容)。

The solution probably lies in a case expression which checks the result returned by :rpc.call/4 in order to defend from errors: 解决方案可能在于case表达式,该表达式检查:rpc.call/4返回的结果以防止错误:

case :rpc.call(node_ap_dev_ejd, :mnesia, :activity, [:async_dirty, f]) do
  {:badrpc, _} -> raise "bad rpc error"
  fbUserList   -> Enum.filter(fbUserList, ...) # and so on
end

I'm having the same issue working mnesia with erlang, the error is because of the anonymous function "f", the thing is that the remote node does not recognize that function because it was created in another node. 我在使用erlang进行mnesia时遇到了相同的问题,该错误是由于匿名函数“ f”引起的,原因是远程节点无法识别该函数,因为它是在另一个节点中创建的。

EDIT: 编辑:

I managed to solve the problem in erlang, I will show you how I did it in erlang, I don't know much about elixir but I´m sure if it can be done in erlang it will in elixir. 我设法用erlang解决了这个问题,我将向您展示我是如何在erlang中完成的,我对elixir的了解不多,但是我敢肯定,如果可以在erlang中完成,那会在elixir中实现。

So this segment 所以这部分

 f = fn()-> :mnesia.dirty_select(:'cz_map',[{{:cz_map, :'$1', :'_',:'_',:'_'},[],[:'$1']}]) end

  fbUserList = :rpc.call(node_ap_dev_ejd,:mnesia,:activity,[:async_dirty,f])

In erlang is like this 在二郎是这样的

f = fun()-> mnesia:dirty_select(cz_map,[{{cz_map, '$1', '_', '_', '_'},[],['$1']}]) end,
fbUserList = rpc:call(node_ap_dev_ejd, mnesia, activity, [async_dirty, f])

Instead declaring an anonymous fun you have to do something like this 而不是声明一个匿名的乐趣,你必须做这样的事情

fbUserList = rpc:call(node_ap_dev_ejd, mnesia, activity, [async_dirty, mnesia:dirty_select/2, [cz_map, [{{cz_map, '$1', '_', '_', '_'},[],['$1']}]]])

You can find a clear explanation here what kind of types can be sent on an erlang message? 您可以在此处找到清晰的说明,可以在erlang消息上发送哪种类型的消息?

I hope this information helps you. 希望这些信息对您有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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