我正在与这段代码作斗争
defmodule MyApp.Eventbridge do
@moduledoc false
require Logger
def send_event_to_eventbridge() do
event = %{
Source: "car-event",
DetailType: "car-created",
Detail: %{
brand: "Renault",
name: "A4",
country: "US"
},
EventBusName: "my-event-bus"
}
event_json = Poison.encode(event)
Logger.info("Send: #{inspect(event_json)}")
case ExAws.EventBridge.put_events(event_json) |> ExAws.request() do
{:ok, response} ->
Logger.info("Successfully: #{inspect(response)}")
{:error, error} ->
Logger.error("Error: #{inspect(error)}")
end
end
end
我不知道如何修复这个错误
** (Protocol.UndefinedError) protocol Enumerable not implemented for {:ok, "{\"EventBusName\":\"my-event-bus\",\"Detail\":{\"country\":\"US\",\"brand\":\"Renault\",\"name\":\"A4\"},\"DetailType\":\"car-created\",\"Source\":\"car-event\"}"} of type Tuple. This protocol is implemented for the following type(s): DBConnection.PrepareStream, DBConnection.Stream, Date.Range, Ecto.Adapters.SQL.Stream, File.Stream, Function, GenEvent.Stream, HashDict, HashSet, IO.Stream, Jason.OrderedObject, List, Map, MapSet, Phoenix.LiveView.LiveStream, Postgrex.Stream, Range, Stream
(elixir 1.14.0) lib/enum.ex:1: Enumerable.impl_for!/1
(elixir 1.14.0) lib/enum.ex:166: Enumerable.reduce/3
(elixir 1.14.0) lib/enum.ex:4307: Enum.map/2
(ex_aws_eventbridge 0.1.1) lib/ex_aws/event_bridge.ex:88: ExAws.EventBridge.put_events/2
(my_app 0.1.0) lib/my_app/eventbridge.ex:22: MyApp.Eventbridge.send_event_to_eventbridge/0
我用Java语言做了测试,我确实在日志中看到了它,但在Elixir中我无法设置事件以在日志中显示它。
Poison.encode/1
,它返回 {:ok, encoded_data}
形式的元组。您可以从元组中提取它,执行以下操作:
{:ok, event_json} = Poison.encode(event)
如果编码不成功,将会引发错误。
Poison.encode!/1
来代替,它直接返回一个值而不是元组,或者如果编码不成功则引发错误。