Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/elixir/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如果密钥不存在,则尝试从redis获取值时出错_Redis_Elixir_Phoenix Framework - Fatal编程技术网

如果密钥不存在,则尝试从redis获取值时出错

如果密钥不存在,则尝试从redis获取值时出错,redis,elixir,phoenix-framework,Redis,Elixir,Phoenix Framework,如果Redis数据库中没有可用记录,此命令将失败 这是控制台中的错误显示 [ 如何解决此问题?如果密钥不存在,GET命令将不会返回错误。它将返回{:ok,nil}: def handle_info(:after_join, socket) do {_, value} = Redix.command(:redix, ["GET", socket.assigns.user]) Presence.track(socket, socket.assigns.user, %{

如果Redis数据库中没有可用记录,此命令将失败

这是控制台中的错误显示 [


如何解决此问题?

如果密钥不存在,
GET
命令将不会返回错误。它将返回
{:ok,nil}

 def handle_info(:after_join, socket) do
     {_, value} = Redix.command(:redix, ["GET", socket.assigns.user])
    Presence.track(socket, socket.assigns.user, %{
      online_at: :os.system_time(:millisecond),
      status: value
    })
    push socket, "presence_state", Presence.list(socket)
    {:noreply, socket}
  end

哪一行是
user\u socket.ex
的第32行?错误不是由于该函数的返回值造成的。有人在
nil
上调用
String.trim/1
。可能
socket.assignments.user
是nil?def connect(%%“user”=>user,“status”=>status},socket)do#Redix.command(:Redix,[“SET”,user,status]))#{,value}=Redix.command(:Redix,[“GET”,user])if(String.strip(status)=“”)do{,value}=Redix.command(:Redix,[“GET”,user])if(String.strip(value)=“”)do-Redix.command(:Redix,[“SET”,user,“online”])end-else{,value}=Redix.command(:Redix,[“GET”,user])if(String.String.strip(value)==String.strip(status))do#32line else Redix.command(:Redix,[“SET”,user,status])end
 [error] GenServer #PID<0.461.0> terminating
** (FunctionClauseError) no function clause matching in PhoenixChat.RoomChannel.handle_info/2
    (phoenix_chat) web/channels/room_channel.ex:14: PhoenixChat.RoomChannel.handle_info(%Phoenix.Socket.Broadcast{event: "message:new", payload: %{body: %{"to" => "rusiru", "value" => "hi bro "}, timestamp: 1501498879699, user: "abc"}, topic: "room:rusiru"}, %Phoenix.Socket{assigns: %{user: "abc"}, channel: PhoenixChat.RoomChannel, channel_pid: #PID<0.461.0>, endpoint: PhoenixChat.Endpoint, handler: PhoenixChat.UserSocket, id: nil, joined: true, pubsub_server: PhoenixChat.PubSub, ref: nil, serializer: Phoenix.Transports.WebSocketSerializer, topic: "room:abc", transport: Phoenix.Transports.WebSocket, transport_name: :websocket, transport_pid: #PID<0.421.0>})
    (phoenix) lib/phoenix/channel/server.ex:239: 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.Broadcast{event: "message:new", payload: %{body: %{"to" => "rusiru", "value" => "hi bro "}, timestamp: 1501498879699, user: "abc"}, topic: "room:rusiru"}
State: %Phoenix.Socket{assigns: %{user: "abc"}, channel: PhoenixChat.RoomChannel, channel_pid: #PID<0.461.0>, endpoint: PhoenixChat.Endpoint, handler: PhoenixChat.UserSocket, id: nil, joined: true, pubsub_server: PhoenixChat.PubSub, ref: nil, serializer: Phoenix.Transports.WebSocketSerializer, topic: "room:abc", transport: Phoenix.Transports.WebSocket, transport_name: :websocket, transport_pid: #PID<0.421.0>}
[info] JOIN room:abc to PhoenixChat.RoomChannel
  Transport:  Phoenix.Transports.WebSocket
  Parameters: %{}
[info] Replied room:abc :ok
 def handle_info(:after_join, socket) do
     {_, value} = Redix.command(:redix, ["GET", socket.assigns.user])
    Presence.track(socket, socket.assigns.user, %{
      online_at: :os.system_time(:millisecond),
      status: value
    })
    push socket, "presence_state", Presence.list(socket)
    {:noreply, socket}
  end
iex(1)> {:ok, conn} = Redix.start_link
{:ok, #PID<0.159.0>}
iex(2)> Redix.command(conn, ["GET", "key-that-doesnt-exist"])
{:ok, nil}
case Redix.command(:redix, ["GET", socket.assigns.user]) do
  {:ok, nil} -> # key doesn't exist
  {:ok, value} -> # key exists, value is in `value`
  {:error, error} -> # some other error
end