我mix fetch.btc在 phoenix 应用程序上有混合任务(lib/mix/tasks/fetch.btc.ex):
defmodule Mix.Tasks.Fetch.Btc do
use Mix.Task
def run(_args) do
res = HTTPoison.get!("https://blockchain.info/ticker")
IO.inspect(res)
end
end
Run Code Online (Sandbox Code Playgroud)
当我运行时mix fetch.btc出现错误:
** (ArgumentError) argument error
(stdlib) :ets.lookup_element(:hackney_config, :mod_metrics, 2)
PROJ_DIR/deps/hackney/src/hackney_metrics.erl:27: :hackney_metrics.get_engine/0
PROJ_DIR/deps/hackney/src/hackney_connect.erl:78: :hackney_connect.create_connection/5
PROJ_DIR/deps/hackney/src/hackney_connect.erl:47: :hackney_connect.connect/5
PROJ_DIR/deps/hackney/src/hackney.erl:330: :hackney.request/5
lib/httpoison/base.ex:787: HTTPoison.Base.request/6
lib/httpoison.ex:128: HTTPoison.request!/5
lib/mix/tasks/fetch.btc.ex:14: Mix.Tasks.Fetch.Btc.run/1
(mix) lib/mix/task.ex:331: Mix.Task.run_task/3
(mix) lib/mix/cli.ex:79: Mix.CLI.run_task/2
(elixir) lib/code.ex:767: Code.require_file/2
Run Code Online (Sandbox Code Playgroud)
但在我的控制器中,这段代码res = HTTPoison.get!("https://blockchain.info/ticker")工作成功!
信息:
hackney: 1.15.1
httpoison: 1.5.0
phoenix: 1.4.3
Run Code Online (Sandbox Code Playgroud)
我想从 if-else 语句的 else 部分调用插头。
我尝试过打电话,plug SpiderWeb.AdminAuth但接听不到ArgumentError: cannot set attribute @plugs inside function/macro.。
我也尝试过SpiderWeb.AdminAuth.call(conn),但出现错误UndefinedFunctionError at GET /user/1: function SpiderWeb.AdminAuth.call/1 is undefined or private.
我也可以在 else 部分重写整个插件,但这违背了 DRY 原则。
这是用户控制器文件:
defmodule SpiderWeb.UserController do
use SpiderWeb, :controller
alias Spider.Accounts
plug :user_authenticate when action in [:index, :show]
plug SpiderWeb.AdminAuth when action in [:index]
## Action parameters changed to ensure only current user has access or admin ##
def action(conn, _) do
args = [conn, conn.params, conn.assigns.current_user] …Run Code Online (Sandbox Code Playgroud) 我在 Elixir 中有两张地图:
mapA = %{"test1" => "result1"}
mapB = %{"test2" => "result2"}
Run Code Online (Sandbox Code Playgroud)
我希望我的输出看起来像这样:
[%{"test1" => "result1"}, %{"test2" => "result2"}]
Run Code Online (Sandbox Code Playgroud) 只是试图删除一个目录。不能,因为not owner错误。
iex(1)> File.mkdir "testing"
:ok
iex(2)> File.rm! "testing"
** (File.Error) could not remove file "testing": not owner
(elixir) lib/file.ex:1128: File.rm!/1
iex(2)> File.rm! "testing/"
** (File.Error) could not remove file "testing/": not owner
(elixir) lib/file.ex:1128: File.rm!/1
iex(2)> File.rm! "./testing/"
** (File.Error) could not remove file "./testing/": not owner
(elixir) lib/file.ex:1128: File.rm!/1
Run Code Online (Sandbox Code Playgroud)
一定是一些简单的事情,我只是没有从文档中正确阅读,对吧?
所以现在我正在使用这个:
dynamic(
[u],
ilike(
u.name, ^"%#{String.replace(term, "%", "\\%")}%"
)
)
Run Code Online (Sandbox Code Playgroud)
该术语是一个简单的字符串,例如"charlie"。我如何将其与术语列表一起使用,例如:["charlie", "dennis", "frank"]- 没有片段是否可能?
我有一个 LiveView 应用程序,可以搜索机场代码。当用户输入时,ham它应该用 (String.upcase/1) 替换该表单字段的内容HAM,但事实并非如此。但根据我对我的代码的理解应该是这样。我需要更改什么才能用大写版本替换该字段中的所有输入?
顺便说一句:如果我向其添加一个按钮并使用phx-submit而不是phx-change. 但我想让它为phx-change.
$ mix phx.new travelagent --live --no-ecto\n$ cd travelagent\nRun Code Online (Sandbox Code Playgroud)\n\nlib/travelagent_web/router.ex
\n\n[...]\nscope "/", TravelagentWeb do\n pipe_through :browser\n\n live "/", PageLive, :index\n live "/search", SearchLive\nend\n[...]\nRun Code Online (Sandbox Code Playgroud)\n\nlib/travelagent/airports.ex
\n\ndefmodule Travelagent.Airports do\n def search_by_code(""), do: []\n\n def search_by_code(code) do\n list_airports()\n |> Enum.filter(&String.starts_with?(&1.code, code))\n end\n\n def list_airports do\n [\n %{name: "Berlin Brandenburg", code: "BER"},\n %{name: "Berlin Sch\xc3\xb6nefeld", code: "SXF"},\n %{name: "Berlin …Run Code Online (Sandbox Code Playgroud) 我正在 Elixir/Phoenix 中实现后端 API。出于安全原因,我们决定在 URL 上显示尽可能少的内容,以便将要执行的操作嵌入到 POST 请求的 JSON 负载中。例如,我们将使用以下有效负载https://my.server.com/api/get_user/42发出 POST ,而不是使用类似 REST 的请求:https://my.server.com/api/
{
"action" : "get_user",
"params" : {
"id" : 42
}
}
Run Code Online (Sandbox Code Playgroud)
到目前为止,我的路由器看起来像这样:
scope "/api", AlaaarmWeb do
pipe_through :api
post "/", ApiController, :process
match :*, "/*path", ApiController, :error_404
end
Run Code Online (Sandbox Code Playgroud)
我有一堆process在地图上进行模式匹配的函数params:
def process(conn, params = %{"action" => "get_user"}) do
# ... get the user from the DB in resp, and sed back
json(conn, resp)
end
Run Code Online (Sandbox Code Playgroud)
它工作正常,但代码不是很清楚:一个大文件,使用相同的控制器, ApiController而有一个UserApiController用于管理用户,一个 …
我想使用以下功能将事件从 liveView 服务器发送到客户端:
\n def handle_event("test", _, socket) do\n {:noreply, push_event(socket, "testEvent", %{coins: 23, user: "user"})}\n end\nRun Code Online (Sandbox Code Playgroud)\n这在 liveview 文档中的解释非常相似。 \n https://hexdocs.pm/phoenix_live_view/js-interop.html \n但是我收到此错误消息:
\n(CompileError) undefined function push_event/3\n\nStacktrace:\n \xe2\x94\x82 (elixir 1.10.4) src/elixir_locals.erl:114: anonymous fn/3 in :elixir_locals.ensure_no_undefined_local/3\n \xe2\x94\x82 (stdlib 3.8) erl_eval.erl:680: :erl_eval.do_apply/6\nRun Code Online (Sandbox Code Playgroud)\n我错过了一些导入吗?或者我做错了什么?
\n如果我有一些 svg 通过分配给变量上的视图svg,我可以使用以下命令渲染到视图中:
<%= @svg %>\nRun Code Online (Sandbox Code Playgroud)\n然而,在 DOM 中,我可以检查页面并智能地看到 phoenix 默认不渲染 SVG,因为它是字符串表示形式。但是,我不知道如何覆盖该默认值,以便我可以正确地注入 dom/渲染 SVG,而不仅仅是在页面中显示字符串,而是实际渲染图像。我该怎么做?我是否必须使用 javascript 来渲染事实\xe2\x80\x94 这也没关系,但仅在服务器端渲染会非常好。
\n非常感谢!
\n我在 postgresql 中有一个表,我想用随机数更新所有记录的“points”列的值。在其他语言中,我们可以循环所有数据库记录,但我如何使用 ecto 来做到这一点?我试过这个:
Repo.all(from u in User, update: User.changeset(u, %{points: :rand.uniform(100)}))
Run Code Online (Sandbox Code Playgroud)
但它输出以下错误:
== Compilation error in file lib/hello_remote/user.ex ==
** (Ecto.Query.CompileError) malformed update `User.changeset(u, %{points: :rand.uniform(100)})` in query expression, expected a keyword list with set/push/pop as keys with field-value pairs as values
expanding macro: Ecto.Query.update/3
lib/hello_remote/user.ex:30: HelloRemote.User.update_points/0
expanding macro: Ecto.Query.from/2
lib/hello_remote/user.ex:30: HelloRemote.User.update_points/0
(elixir 1.10.4) lib/kernel/parallel_compiler.ex:304: anonymous fn/4 in Kernel.ParallelCompiler.spawn_workers/7
Run Code Online (Sandbox Code Playgroud)
我也尝试过这个:
from(u in User)
|> Repo.update_all(set: [points: Enum.random(0..100)])
Run Code Online (Sandbox Code Playgroud)
但它会用相同的值更新所有记录
elixir ×10
ecto ×2
dom ×1
elixir-mix ×1
httpoison ×1
httprequest ×1
javascript ×1
maps ×1
postgresql ×1