我有一个Statistics
控制器,它获取date_from
与date_to
计算之间进行收入的订单数量和金额date_from
和date_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
您未正确匹配地图名称.在您要发送到服务器的参数中,您正在使用
%{"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"
.
您可以在客户端上修复它以发送正确的密钥,也可以更改服务器以匹配您发送的内容.