use*_*858 2 elixir phoenix-framework
我有message = %{"to" => "testuser", "value" => "asdads"}
地图.我需要访问此地图中"to"键的值
message.to
message[:to]
Map.fetch!(message, to)
Run Code Online (Sandbox Code Playgroud)
到目前为止没什么用
这是控制台错误消息
[error] GenServer #PID<0.395.0> terminating
** (KeyError) key :to not found in: %{"to" => "testuser", "value" => "aadadadad"}
(phoenix_chat) web/channels/room_channel.ex:31: PhoenixChat.RoomChannel.handle_in/3
(phoenix) lib/phoenix/channel/server.ex:225: anonymous fn/4 in Phoenix.Channel.Server.handle_info/2
(stdlib) gen_server.erl:601: :gen_server.try_dispatch/4
(stdlib) gen_server.erl:667: :gen_server.handle_msg/5
(stdlib) proc_lib.erl:247: :proc_lib.init_p_do_apply/3
Last message: %Phoenix.Socket.Message{event: "message:new", payload: %{"to" => "testuser", "value" => "aadadadad"}, ref: "4", topic: "room:Pamidu"}
State: %Phoenix.Socket{assigns: %{user: "Pamidu"}, channel: PhoenixChat.RoomChannel, channel_pid: #PID<0.395.0>, endpoint: PhoenixChat.Endpoint, handler: PhoenixChat.UserSocket, id: nil, joined: true, pubsub_server: PhoenixChat.PubSub, ref: nil, serializer: Phoenix.Transports.WebSocketSerializer, topic: "room:Pamidu", transport: Phoenix.Transports.WebSocket, transport_name: :websocket, transport_pid: #PID<0.384.0>}
Run Code Online (Sandbox Code Playgroud)
你的地图的键是字符串,而不是原子.您发布的所有三个代码段都将访问密钥:to
(atom),而不是"to"
(字符串).
您可以message["to"]
访问该值.Map.fetch!(message, "to")
如果要在值不存在时引发错误,也可以执行此操作.
归档时间: |
|
查看次数: |
476 次 |
最近记录: |