当前位置:  开发笔记 > 前端 > 正文

POST时的Phoenix.ActionClauseError,对Myapp.StatisticsController.calculate的错误请求

如何解决《POST时的Phoenix.ActionClauseError,对Myapp.StatisticsController.calculate的错误请求》经验,为你挑选了1个好方法。

我有一个Statistics控制器,它获取date_fromdate_to计算之间进行收入的订单数量和金额date_fromdate_to.

def calculate(conn,  %{"statistics" => %{"date_from" => date_from, "date_to" => date_to}}) do

revenue = Repo.one(from p in Order, where: p.created_date >= ^date_from and p.created_date <= ^date_to, select: sum(p.paied))
order = Repo.one(from p in Order, where: p.created_date >= ^date_from and p.created_date <= ^date_to, select: count(p.id))

conn
|> put_resp_content_type("application/json")
|> render("statistics.json", order: order, revenue: revenue)

end

以下是statistics.view和router,它会导致错误.

  def render("statistics.json", %{order: order, revenue: revenue}) do

%{
  order: order

}

%{
  revenue: revenue
}

end

statistics_path POST /api/v1/statistics Myapp.StatisticsController :calculate

我在POST时读过Phoenix.ActionClauseError,没有匹配的动作子句来处理请求和对Lxphnx.ArticleController.create的错误请求,没有匹配的动作子句来处理请求.但我找不到如何解决这个问题......

我怎样才能使它工作?我在这里错过了什么?

提前致谢.

--EDIT ERROR

[info] POST /api/v1/statistics [debug] QUERY OK source="users" db=0.4ms SELECT u0."id", u0."username", u0."encrypted_password", u0."first_name", u0."last_name", u0."role_id", u0."inserted_at", u0."updated_at" FROM "users" AS u0 WHERE (u0."id" = $1) [1] [debug] Processing by Pos8.StatisticsController.calculate/2 Parameters: %{"statistics" => %{"from" => "2017-01-14T00:00:00.000Z", "to" => "2017-01-27T00:00:00.000Z"}} Pipelines: [:api]

** (Phoenix.ActionClauseError) bad request to Pos8.StatisticsController.calculate, no matching action clause to process request (pos8) web/controllers/api/v1/statistics_controller.ex:11: Pos8.StatisticsController.calculate(%Plug.Conn{adapter: {Plug.Adapters.Cowboy.Conn, :...}, assigns: %{}, before_send: [#Function<1.25719536/1 in Plug.Logger.call/2>, #Function<0.88006395/1 in Phoenix.LiveReloader.before_send_inject_reloader/2>], body_params: %{"statistics" => %{"from" => "2017-01-14T00:00:00.000Z", "to" => "2017-01-27T00:00:00.000Z"}}, cookies: %Plug.Conn.Unfetched{aspect: :cookies}, halted: false, host: "localhost", method: "POST", owner: #PID<0.656.0>, params: %{"statistics" => %{"from" => "2017-01-14T00:00:00.000Z", "to" => "2017-01-27T00:00:00.000Z"}}, path_info: ["api", "v1", "statistics"], path_params: %{}, peer: {{127, 0, 0, 1}, 47000}, port: 4000, private: %{Pos8.Router => {[], %{}}, :guardian_default_claims => {:ok, %{"aud" => "User:1", "exp" => 1485242427, "iat" => 1485156027, "iss" => "Cloud8_POS", "jti" => "6ea4bb17-277e-48c5-ac3e-1d8fbd1798b7", "pem" => %{}, "sub" => "User:1", "typ" => "token"}}, :guardian_default_jwt => "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJhdWQiOiJVc2VyOjEiLCJleHAiOjE0ODUyNDI0MjcsImlhdCI6MTQ4NTE1NjAyNywiaXNzIjoiQ2xvdWQ4X1BPUyIsImp0aSI6IjZlYTRiYjE3LTI3N2UtNDhjNS1hYzNlLTFkOGZiZDE3OThiNyIsInBlbSI6e30sInN1YiI6IlVzZXI6MSIsInR5cCI6InRva2VuIn0.teJ6Z-4Edf98MZdidk2PjzmkLC2yzJ2o4hh2CXhbs0A", :guardian_default_resource => %Pos8.User{__meta__: #Ecto.Schema.Metadata<:loaded, "users">, encrypted_password: "$2b$12$OwRsY4HUsPG4VIRTZmv2ouXnN4Ww0d4fM.0h6ylXPCS/5ntyp.kk6", first_name: "Staff", id: 1, inserted_at: ~N[2017-01-20 07:27:44.509052], last_name: "pos", orders: #Ecto.Association.NotLoaded, password: nil, role: #Ecto.Association.NotLoaded, role_id: 2, updated_at: ~N[2017-01-20 07:27:44.509059], username: "staff_pos"}, :phoenix_action => :calculate, :phoenix_controller => Pos8.StatisticsController, :phoenix_endpoint => Pos8.Endpoint, :phoenix_format => "json", :phoenix_layout => {Pos8.LayoutView, :app}, :phoenix_pipelines => [:api], :phoenix_route => #Function<14.65451164/1 in Pos8.Router.match_route/4>, :phoenix_router => Pos8.Router, :phoenix_view => Pos8.StatisticsView, :plug_session_fetch => #Function<1.46198565/1 in Plug.Session.fetch_session/1>}, query_params: %{}, query_string: "", remote_ip: {127, 0, 0, 1}, req_cookies: %Plug.Conn.Unfetched{aspect: :cookies}, req_headers: [{"host", "localhost:4000"}, {"user-agent", "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:50.0) Gecko/20100101 Firefox/50.0"}, {"accept", "application/json"}, {"accept-language", "en-US,en;q=0.5"}, {"accept-encoding", "gzip, deflate"}, {"content-type", "application/json"}, {"authorization", "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJhdWQiOiJVc2VyOjEiLCJleHAiOjE0ODUyNDI0MjcsImlhdCI6MTQ4NTE1NjAyNywiaXNzIjoiQ2xvdWQ4X1BPUyIsImp0aSI6IjZlYTRiYjE3LTI3N2UtNDhjNS1hYzNlLTFkOGZiZDE3OThiNyIsInBlbSI6e30sInN1YiI6IlVzZXI6MSIsInR5cCI6InRva2VuIn0.teJ6Z-4Edf98MZdidk2PjzmkLC2yzJ2o4hh2CXhbs0A"}, {"origin", "http://localhost:4000"}, {"referer", "http://localhost:4000/statistics"}, {"content-length", "82"}, {"dnt", "1"}, {"connection", "keep-alive"}], request_path: "/api/v1/statistics", resp_body: nil, resp_cookies: %{}, resp_headers: [{"cache-control", "max-age=0, private, must-revalidate"}, {"x-request-id", "bmu8b4j5us3e1ajk0ip7qdemph58o471"}], scheme: :http, script_name: [], secret_key_base: "la28avUDdDxcVfvvoPEv/orTz1eK6kcB3Z1WylyKujOE0Wz6EXRz/Ek4QT414zSy", state: :unset, status: nil}, %{"statistics" => %{"from" => "2017-01-14T00:00:00.000Z", "to" => "2017-01-27T00:00:00.000Z"}}) (pos8) web/controllers/api/v1/statistics_controller.ex:1: Pos8.StatisticsController.action/2 (pos8) web/controllers/api/v1/statistics_controller.ex:1: Pos8.StatisticsController.phoenix_controller_pipeline/2 (pos8) lib/pos8/endpoint.ex:1: Pos8.Endpoint.instrument/4 (pos8) lib/phoenix/router.ex:261: Pos8.Router.dispatch/2 (pos8) web/router.ex:1: Pos8.Router.do_call/2 (pos8) lib/pos8/endpoint.ex:1: Pos8.Endpoint.phoenix_pipeline/1 (pos8) lib/plug/debugger.ex:123: Pos8.Endpoint."call (overridable 3)"/2 (pos8) lib/pos8/endpoint.ex:1: Pos8.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



1> Justin Wood..:

您未正确匹配地图名称.在您要发送到服务器的参数中,您正在使用

%{"statistics" => %{"from" => "2017-01-14T00:00:00.000Z", "to" => "2017-01-27T00:00:00.000Z"}}

在您的calculate/2功能定义中,您正在使用

%{"statistics" => %{"date_from" => date_from, "date_to" => date_to}}

你试图匹配"date_from"它应该只是"from".这同样适用于"date_to""to".

您可以在客户端上修复它以发送正确的密钥,也可以更改服务器以匹配您发送的内容.

推荐阅读
吻过彩虹的脸_378
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有