失败的代码:
Enum.map(1..30, &(&1 * 2)) |> Enum.map &(&1 + 1) |> Enum.sum
** (Protocol.UndefinedError) protocol Enumerable not implemented for 3
(elixir) lib/enum.ex:1: Enumerable.impl_for!/1
(elixir) lib/enum.ex:112: Enumerable.reduce/3
(elixir) lib/enum.ex:1400: Enum.reduce/3
(elixir) lib/enum.ex:1043: anonymous fn/3 in Enum.map/2
(elixir) lib/enum.ex:1387: Enum."-reduce/3-lists^foldl/2-0-"/3
(elixir) lib/enum.ex:1043: Enum.map/2
Run Code Online (Sandbox Code Playgroud)
但这完全正常:
iex(18)> arr = Enum.map(1..30, &(&1 * 2)) |> Enum.map &(&1 + 1)
[3, 5, 7, 9, 11, 13, 15, 17, 19, 21, 23, 25, 27, 29, 31, 33, 35, 37, 39, 41, 43,
45, 47, 49, 51, 53, …Run Code Online (Sandbox Code Playgroud) 我正在尝试获取特定列的总和.我有一个订单模式,字段总数,存储总价格.现在我正在尝试创建一个查询,它将总计所有订单的总价值,但不确定我是否做得对.这是我到目前为止:
def create(conn, %{"statistic" => %{"date_from" => %{"day" => day_from, "month" => month_from, "year" => year_from}}}) do
date_from = Ecto.DateTime.cast!({{year_from, month_from, day_from}, {0, 0, 0, 0}})
revenue = Repo.all(from p in Order, where: p.inserted_at >= ^date_from, select: sum(p.total))
render(conn, "result.html", revenue: revenue)
end
Run Code Online (Sandbox Code Playgroud)
只是<%= @revenue %>在html.eex中调用它.截至目前,它不会返回错误,只是在页面上呈现随机符号,而不是总收入.
我认为我的查询是错误的,但无法找到有关如何使其正常工作的良好信息.任何帮助表示感谢,谢谢!
我想在我的聊天应用程序中使用{:redix,"〜> 0.6.1"} hex包,并从监督树开始
{:ok, conn} = Redix.start_link()
{:ok, conn} = Redix.start_link(host: "example.com", port: 5000)
{:ok, conn} = Redix.start_link("redis://localhost:6379/3", name: :redix)
Redix.command(conn, ["SET", "mykey", "foo"])
Run Code Online (Sandbox Code Playgroud)
但是当我尝试将连接开始链接放到子进程时它会出错
children = [
# Start the Ecto repository
supervisor(PhoenixChat.Repo, []),
# Start the endpoint when the application starts
supervisor(PhoenixChat.Endpoint, []),
# Start your own worker by calling: PhoenixChat.Worker.start_link(arg1, arg2, arg3)
# worker(PhoenixChat.Worker, [arg1, arg2, arg3]),
supervisor(PhoenixChat.Presence, []),
#supervisor(Phoenix.PubSub.Redis, [:chat_pubsub, host: "127.0.0.1"])
]
Run Code Online (Sandbox Code Playgroud)
如何启动redix连接并将数据存储到Redis?
我正在阅读《凤凰城指南》 1.4.6,并且正在“ 存在”这一章中。因此,我遵循本章的所有步骤,而我的控制台javascript对此有所抱怨:
app.js:1 Uncaught Error: Module build failed: SyntaxError: /Users/romenigld/workspace/phoenix/hello/assets/js/app.js: Identifier 'socket' has already been declared (21:4)
19 | import {Socket, Presence} from "phoenix"
20 |
> 21 | let socket = new Socket("/socket", {
| ^
22 | params: {user_id: window.location.search.split("=")[1]}
23 | })
24 |
at Parser.raise (:4000/Users/romenigld/workspace/phoenix/hello/assets/node_modules/@babel/parser/lib/index.js:6322)
at ScopeHandler.checkRedeclarationInScope (:4000/Users/romenigld/workspace/phoenix/hello/assets/node_modules/@babel/parser/lib/index.js:3754)
at ScopeHandler.declareName (:4000/Users/romenigld/workspace/phoenix/hello/assets/node_modules/@babel/parser/lib/index.js:3720)
at Parser.checkLVal (:4000/Users/romenigld/workspace/phoenix/hello/assets/node_modules/@babel/parser/lib/index.js:8006)
at Parser.parseVarId (:4000/Users/romenigld/workspace/phoenix/hello/assets/node_modules/@babel/parser/lib/index.js:10441)
at Parser.parseVar (:4000/Users/romenigld/workspace/phoenix/hello/assets/node_modules/@babel/parser/lib/index.js:10412)
at Parser.parseVarStatement (:4000/Users/romenigld/workspace/phoenix/hello/assets/node_modules/@babel/parser/lib/index.js:10234)
at Parser.parseStatementContent (:4000/Users/romenigld/workspace/phoenix/hello/assets/node_modules/@babel/parser/lib/index.js:9830)
at Parser.parseStatement (:4000/Users/romenigld/workspace/phoenix/hello/assets/node_modules/@babel/parser/lib/index.js:9763)
at Parser.parseBlockOrModuleBlockBody …Run Code Online (Sandbox Code Playgroud) 我的Elixir代码中有一个特定的日志语句,应该经常调用它,但是我不想每次都打印该日志语句。可能是我想每5次打印一次。
如果有办法请帮助。它以太快的速度淹没了我的日志文件!提前致谢