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

时间:2017-01-23 10:30:59

标签: elixir phoenix-framework

我有一个Statistics控制器,可获得date_fromdate_to来计算date_fromdate_to之间的订单数量和收入金额。< / p>

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

我已阅读Phoenix.ActionClauseError at POST, no matching action clause to process requestbad request to Lxphnx.ArticleController.create, no matching action clause to process request。但我找不到如何解决这个问题...

我怎样才能让它发挥作用?我在这里缺少什么?

提前致谢。

- 编辑错误

[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<association :orders is not loaded>, password: nil, role: #Ecto.Association.NotLoaded<association :role is not loaded>, 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 个答案:

答案 0 :(得分:6)

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

%{"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"同样如此。

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