Plug.Conn,所有插头必须接收一个连接 (conn) 和 return 一个连接

Plug.Conn, all plugs must receive a connection (conn) and return a connection

所以我试图通过在回调中匹配 params["state"] 来更新模式,在我的控制器中,我正在做这样的事情。

def create(conn, params) do
  viewer = GiftActions.get_gift_user!(params["user_id"])

  case params["state"] do
    "captured" ->
      GiftActions.validate_gift_order(viewer, params)
      Logger.info("Request is validated sending code to #{inspect(viewer.email)}")

      {:ok, id} ->
        put_status(conn, 200)
        |> json(%{data: id})

      _ -> put_status(conn, 404) |> json(%{data: ""})
  end
end

如果 params["state"]captured,我的函数将处理更新,就像这样

def validate_gift_order(viewer, attrs) do
 {:ok, gift_order} = __MODULE__.get_gift_by_payment_id(viewer, attrs["id"])

 gift_order
 |> GiftOrders.changeset(%{state: attrs["state"]})
 |> Repo.update()
end

一切正常,但最后我没能做到 return Plug.Conn。错误是

[error] #PID<0.5693.0> running VWeb.Endpoint (connection #PID<0.5692.0>, stream id 1) terminated
Server: localhost:4000 (http)
Request: POST /api/v1/gift-callback?user_id=#######
** (exit) an exception was raised:
    ** (RuntimeError) expected action/2 to return a Plug.Conn, all plugs must receive a connection (conn) and return a connection, got: :ok
        (viavolo 0.1.0) lib/v_web/controllers/gif_callback_controller.ex:1: VWeb.GiftCallbackController.phoenix_controller_pipeline/2
        (phoenix 1.5.9) lib/phoenix/router.ex:352: Phoenix.Router.__call__/2
        (viavolo 0.1.0) lib/viavolo_web/endpoint.ex:1: ViavoloWeb.Endpoint.plug_builder_call/2
        (viavolo 0.1.0) lib/viavolo_web/endpoint.ex:1: VWeb.Endpoint."call (overridable 3)"/2
        (viavolo 0.1.0) lib/plug/debugger.ex:136: VWeb.Endpoint."call (overridable 4)"/2
        (viavolo 0.1.0) lib/v_web/endpoint.ex:1: VWeb.Endpoint.call/2

我不完全确定为什么?我应该在这里 return 做什么?

这个问题我会引用一段话来回答,剩下的你们自己找here !

堆栈跟踪告诉您您的控制器操作未返回 Plug.Conn 结构。在 Elixir 中,返回函数最后一个表达式的结果。查看函数的最后一行并确保它返回 case 表达式

的结果

你的代码缩进错误,这让我觉得你认为你有另一个 case 声明。这是您的代码的正确缩进:

def create(conn, params) do
  viewer = GiftActions.get_gift_user!(params["user_id"])

  case params["state"] do
    "captured" ->
      GiftActions.validate_gift_order(viewer, params)
      Logger.info("Request is validated sending code to #{inspect(viewer.email)}")

    {:ok, id} ->
      put_status(conn, 200)
      |> json(%{data: id})

    _ -> put_status(conn, 404) |> json(%{data: ""})
  end
end

所以,当 params["state"]"captured" 时,你的最后一个命令是 Logger.info,它 return 不是 Plug.Conn

您似乎打算在 validate_gift_order 的 return 上添加 case:

def create(conn, params) do
  viewer = GiftActions.get_gift_user!(params["user_id"])

  case params["state"] do
    "captured" ->
      validation = GiftActions.validate_gift_order(viewer, params)
      Logger.info("Request is validated sending code to #{inspect(viewer.email)}")

      case validation do
        {:ok, id} ->
          put_status(conn, 200)
          |> json(%{data: id})

        _ -> put_status(conn, 404) |> json(%{data: ""})
      end
  end
end