在URI.encode_query(表单)中映射“语法错误之前”(第一个元素)

时间:2017-03-18 18:01:32

标签: maps elixir

我已经做了this几天了,我遇到了这个问题:

每当我尝试将地图编码为查询字符串时,我都会收到错误“语法错误:chat_id”

form = %{
      "chat_id" => 237799109,
      "text" => "OMG a message"
    }

{status, body} = URI.encode_query(form)

#=> (SyntaxError) lib/elixir.ex:20: syntax error before: chat_id

但据我所知这是地图语法,不是吗?正如here所示,这个例子出现了:

iex> hd = %{"foo" => 1, "bar" => 2}
iex> URI.encode_query(hd)
"bar=2&foo=1

这里发生了什么?

完整的错误消息:

== Compilation error on file lib/elixir.ex ==
** (SyntaxError) lib/elixir.ex:20: syntax error before: chat_id
    (elixir) lib/kernel/parallel_compiler.ex:117: anonymous fn/4 in Kernel.Paral
lelCompiler.spawn_compilers/1

1 个答案:

答案 0 :(得分:0)

我不知道你为什么会得到你列出的错误,但URI.encode_query/1只返回一个binary参数。您正尝试将其与tuple进行模式匹配。

您可以粘贴更多代码而不仅仅是那两行吗?

iex(2)> URI.encode_query(form)
"chat_id=237&text=OMG+a+message"