IEx.pry for debugging Argument error :erlang.binary_to_integer

IEx.pry for debugging Argument error :erlang.binary_to_integer

当我在玩这里 https://github.com/ryanswapp/react-phoenix-starter-template 的 Phoenix 入门示例时,我 运行 遇到了这个错误,我很难调试它。

Compiled web/connection.ex
[info] OPTIONS /api/v1/current_user
[info] Sent 204 in 74µs
Compiled web/connection.ex
[info] GET /api/v1/current_user
[info] Sent 500 in 23ms
[error] #PID<0.418.0> running ReactPhoenix.Endpoint terminated
Server: localhost:4000 (http)
Request: GET /api/v1/current_user?jwt=eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJzdWIiOiJVc2VyOmMyZDBmZGQ5LWJkNTUtNDJkNS1iMmJlLTAzYjRkNThkOTg0YyIsInBlbSI6e30sImp0aSI6IjAxZGIxNjQ2LTZmZGEtNGI3Yy04YTI1LTRhZWFhYjAzMjE0NiIsImlzcyI6IlJlYWN0UGhvZW5peCIsImlhdCI6MTQ1NDI1MjE4NCwiZXhwIjoxNDU0NTExMzg0LCJhdWQiOiJ0b2tlbiJ9.YiGd-r2g7w7DiAvicq2C0uqhrwM4ZnUXRlnLg53GLag
** (exit) an exception was raised:
    ** (ArgumentError) argument error
        :erlang.binary_to_integer("c2d0fdd9-bd55-42d5-b2be-03b4d58d984c")
        (react_phoenix) lib/react_phoenix/guardian_serializer.ex:10: ReactPhoenix.GuardianSerializer.from_token/1
        lib/guardian/plug/load_resource.ex:27: Guardian.Plug.LoadResource.call/2 
        (react_phoenix) web/router.ex:13: ReactPhoenix.Router.api/2
        (react_phoenix) web/router.ex:1: ReactPhoenix.Router.match/4
        (react_phoenix) web/router.ex:1: ReactPhoenix.Router.do_call/2
        (react_phoenix) lib/react_phoenix/endpoint.ex:1: ReactPhoenix.Endpoint.phoenix_pipeline/1
        (react_phoenix) lib/plug/debugger.ex:93: ReactPhoenix.Endpoint."call (overridable 3)"/2
        (react_phoenix) lib/phoenix/endpoint/render_errors.ex:34: ReactPhoenix.Endpoint.call/2
        (plug) lib/plug/adapters/cowboy/handler.ex:15: Plug.Adapters.Cowboy.Handler.upgrade/4
        (cowboy) src/cowboy_protocol.erl:442: :cowboy_protocol.execute/4

错误消息指向的模块如下。我尝试了 IEx.pry(如注释掉的代码所示),但即使服务器是通过 iex -S mix phoenix.server 启动的,它也不会暂停检查代码。

defmodule ReactPhoenix.GuardianSerializer do
require IEx
  @behaviour Guardian.Serializer

  alias ReactPhoenix.Repo
  alias ReactPhoenix.User

  def for_token(user = %User{}), do: { :ok, "User:#{user.id}" }
  def for_token(_), do: { :error, "Unknown resource type" }

  def from_token("User:" <> id), do: { :ok, Repo.get(User, String.to_integer(id)) }   # This is line 10

  #def from_token("User:" <> id), do
  #  IEx.pry
  #  IO.inspect id
  #  { :ok, Repo.get(User, String.to_integer(id)) }
  #  end

  def from_token(_), do: { :error, "Unknown resource type" }
end
(react_phoenix) lib/react_phoenix/guardian_serializer.ex:10: ReactPhoenix.GuardianSerializer.from_token/1

编辑:我刚刚意识到我很傻,并且昏昏沉沉地读了这篇文章。实际上,它看起来像是将一个字符串(UUID)强制转换为一个整数,而这永远不会起作用,因为它不是字符串的整数表示。如果您还没有弄清楚,请发表评论,或者打扰我,但如果需要,我会帮助您进一步调试。

这一行似乎表明此时您使用的是字符列表,而您应该使用字符串,反之亦然。在 Elixir 中," 字符串被认为是二进制文件,而 ' 被认为是字符列表。

String.to_char_list

String.to_string

应该能够帮助您解决问题并在上面引用的地方进行转换。如果您在该行之前放置 IEx.pry,您应该能够加入并测试它。