0
我得到這個錯誤試圖解析字符串爲float:藥劑 - 解析字符串浮動導致錯誤
case insert_product_shop(conn, existing_product.id, existing_shop.id, String.to_float(posted_product["price"])) do
錯誤:
20:45:29.766 [error] #PID<0.342.0> running Api.Router terminated
Server: 172.20.10.2:4000 (http)
Request: POST /products
** (exit) an exception was raised:
** (ArgumentError) argument error
:erlang.binary_to_float(58.25)
(api) lib/api/router.ex:120: anonymous fn/1 in Api.Router.do_match/4
(api) lib/api/router.ex:1: Api.Router.plug_builder_call/2
(api) lib/plug/debugger.ex:123: Api.Router.call/2
(plug) lib/plug/adapters/cowboy/handler.ex:15: Plug.Adapters.Cowboy.Handler.upgrade/4
(cowboy) /Users/Ben/Development/Projects/vepo/api/deps/cowboy/src/cowboy_protocol.erl:442: :cowboy_protocol.exe
cute/4
當我改變
case insert_product_shop(conn, existing_product.id, existing_shop.id, Float.parse(posted_product["price"])) do
:
我收到:
20:54:12.769 [error] #PID<0.336.0> running Api.Router terminated
Server: 172.20.10.2:4000 (http)
Request: POST /products
** (exit) an exception was raised:
** (ArgumentError) argument error
:erlang.binary_to_float(24)
(api) lib/api/router.ex:82: anonymous fn/1 in Api.Router.do_match/4
(api) lib/api/router.ex:1: Api.Router.plug_builder_call/2
(api) lib/plug/debugger.ex:123: Api.Router.call/2
(plug) lib/plug/adapters/cowboy/handler.ex:15: Plug.Adapters.Cowboy.Handler.upgrade/4
(cowboy) /Users/Ben/Development/Projects/vepo/api/deps/cowboy/src/cowboy_protocol.erl:442: :cowboy_protocol.exe
cute/4
如何正確解析字符串以便浮動?我也可以允許它是一個字符串或浮動,如果它是字符串,解析爲浮動?
根據錯誤消息,您已經有一個浮點數。只需使用'posting_product [「price」]''。 – Dogbert