-1

所以我试图通过匹配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

一切正常,但最后我没有回来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

我不完全确定为什么?我应该回到这里做什么?

4

2 回答 2

1

你的代码缩进错误,这让我觉得你认为你有另一个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"]is时"captured",您的最后一个命令是Logger.info,它不返回Plug.Conn.

看起来你打算有一个caseonvalidate_gift_order的回报:

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
于 2022-02-10T17:00:07.473 回答
-2

我会用引用来回答这个问题,你可以在这里找到其余的!

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

于 2022-02-10T16:17:10.230 回答