我需要让GenServer监视器成为一项任务,因此,我这样做:
GenServer.call(server_pid, {:monitor_task, self()})
Run Code Online (Sandbox Code Playgroud)
在服务器中:
def handle_call({:monitor_task, task_pid}, _from, state) do
ref = Process.monitor(task_pid)
{:reply, ref, state}
end
Run Code Online (Sandbox Code Playgroud)
但是,我收到错误:
** (stop) exited in: GenServer.call(#PID<0.768.0>, {:monitor_task, #PID<0.849.0>}, 5000)
** (EXIT) bad return value: :ok
Run Code Online (Sandbox Code Playgroud)
任何的想法?
在我的Phoenix应用程序中,我试图使用HTTPoison HTTP客户端(https://hexdocs.pm/httpoison/api-reference.html)向AgileCRM API发出发布请求。我能够使用cURL发出成功的请求,但是我尝试在Phoenix中复制它的尝试失败,出现401 UNAUTHORIZED错误。
我成功的cURL:
$ curl https://domain.agilecrm.com/dev/api/contacts/search/email \
-H "Accept: application/json" \
-d 'email_ids=["contact@test.com"]' \
-u admin@test.com:api_key
Run Code Online (Sandbox Code Playgroud)
返回状态200和请求的数据。
我失败的HTTPoison:
url = "https://domain.agilecrm.com/dev/api/contacts/search/email"
body = Poison.encode!(%{"body": "email_ids=['contact@test.com']"})
headers = [{"Accept", "application/json"}, {"Authorization", "admin@test.com:api_key"}]
response = HTTPoison.post!(url, body, headers)
IO.inspect response
Run Code Online (Sandbox Code Playgroud)
哪个返回
%HTTPoison.Response{body: "<html><head>\n<meta http-equiv=\"content-type\" content=\"text/html;charset=utf-8\">\n<title>401 UNAUTHORIZED</title>\n</head>\n<body text=#000000 bgcolor=#ffffff>\n<h1>Error: UNAUTHORIZED</h1>\n</body></html>\n",
headers: [{"X-TraceUrl", "/appstats/details?time=1509129565372&type=json"},
{"WWW-Authenticate", "Basic realm=\"agilecrm\""},
{"Content-Type", "text/html; charset=utf-8"},
{"X-Cloud-Trace-Context", "8de994n2tbu2o356891bc3e6"},
{"Date", "Fri, 27 Oct 2017 18:39:25 GMT"}, {"Server", "Google Frontend"},
{"Content-Length", "200"}],
request_url: "https://domain.agilecrm.com/dev/api/contacts/search/email", status_code: …Run Code Online (Sandbox Code Playgroud) 在Elixir中,"="运算符是"绑定"运算符.所以我想知道为什么这段代码有效:
a = 0
a = a + 1
Run Code Online (Sandbox Code Playgroud)
如果我们检查"a"的值现在是1.我本来希望看到一个绑定错误消息,如:
** (MatchError) no match of right hand side value: 0
Run Code Online (Sandbox Code Playgroud)
如何重新绑定操作?
我知道这两个error_logger和error_logger_tty_h是gen_event的交换处理error_logger.
从他们的源代码中,我知道error_logger报告消息最终结束erlang:display,并error_logger_tty_h以`io:format(user,String,Args)结束
我感到困惑的,有什么区别error_logger,并error_logger_tty_h为目的?
我想知道为什么我们需要.()在使用管道运算符时添加,如果没有调用该函数并且只接收一个参数?
id = &(&1)
"Hello" |> id.() |> upcase # HELLO
Run Code Online (Sandbox Code Playgroud)
预期:
id = &(&1)
"Hello" |> id. |> upcase # "undefined function String.upcase/0"
Run Code Online (Sandbox Code Playgroud)
为什么不起作用?我想解释一下Elixir的行为方式.
让我们假设我有一张桌子"customers".我想获得该表的外键.
我们可以使用model.__ struct .__ meta` 从模型中获取表名
我们还可以通过加载所有模块从表中获取模型名称,并使模型模式与表名匹配
这可能是我们从表中获取外键吗?
这样做的最佳方法是什么?
如果它可能在ecto?
谢谢.
我只是尝试使用Postgrex而没有任何类型的ecto设置,所以只是文档自述文件中的示例.
这是我的模块的样子:
defmodule Receive do
def start(_type, _args) do
{:ok, pid} = Postgrex.start_link(
hostname: "localhost",
username: "john",
# password: "",
database: "property_actions",
extensions: [{Postgrex.Extensions.JSON}]
)
Postgrex.query!(
pid,
"INSERT INTO actions (search_terms) VALUES ($1)",
[
%{foo: 'bar'}
]
)
end
end
Run Code Online (Sandbox Code Playgroud)
当我运行代码时,我得到了
** (RuntimeError) type `json` can not be handled by the types module Postgrex.DefaultTypes, it must define a `:json` library in its options to support JSON types
Run Code Online (Sandbox Code Playgroud)
有什么我没有正确设置?根据我在文档中收集的内容,我甚至不需要具有该扩展行,因为默认情况下处理json.
我想Code.format_string!/2在我的escript代码中使用Elixir 1.6.为了兼容不同的Elixir版本,我计划function_exported?/3用来查看用户环境中是否支持format_string.但我发现它不能按预期工作.它总是false在Macbook中返回(Elixir是1.6),但它可以正常调用.
我创建了一个演示来描述这个问题:
复制受给定长度限制的列表模式的最佳方法是什么?
例如:
给定一个模式[1,2,3]和长度7,结果将是[1, 2, 3, 1, 2, 3, 1]
我想在有效检查后返回,然后转到其他页面(1)。然而,似乎之后,然后呈现索引页面(2)。
def index(conn, %{"id" => id}) do
unless Model.is_valid!(id) do
conn
|> redirect(to: other_path(conn, :other)) #(1)
end
# More process are after this.
render(conn, "index.html") #(2)
end
Run Code Online (Sandbox Code Playgroud)
似乎一次重定向了其他页面,但之后继续进行。我怎样才能像其他语言一样以回程停止?