Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/elixir/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
elixir-外部变更集成功和失败代码不再运行_Elixir_Ecto - Fatal编程技术网

elixir-外部变更集成功和失败代码不再运行

elixir-外部变更集成功和失败代码不再运行,elixir,ecto,Elixir,Ecto,此代码用于向messages变量添加字符串,然后在http响应中将其发送回。现在,没有任何内容添加到messages变量中。:ok和:error块中的代码行不再运行。我现在应该在哪里处理错误和成功案例,以添加关于错误和关于成功的消息 messages = {} changeset = Api.UserProduct.changeset( user_product, %{:voted_vegan => true} ) case Api.Repo.update(ch

此代码用于向messages变量添加字符串,然后在http响应中将其发送回。现在,没有任何内容添加到messages变量中。:ok和:error块中的代码行不再运行。我现在应该在哪里处理错误和成功案例,以添加关于错误和关于成功的消息

  messages = {}

  changeset = Api.UserProduct.changeset(
    user_product, %{:voted_vegan => true}
  )

  case Api.Repo.update(changeset) do
      {:ok, product} -> 
        IO.puts("appending messages7")
        messages = Tuple.append(messages, "Product updated")
      {:error, changeset} -> 
        IO.puts("appending messages8")
        messages = Tuple.append(messages, "Product not updated")
  end

  conn
      |> put_resp_content_type("application/json")
      |> send_resp(200, Poison.encode!(%{
          successs: "success",
          errors: Tuple.to_list(messages)
      }))

您的case表达式没有副作用,您应该将case表达式的返回值指定给messages变量,该值将是已执行分支中最后一个表达式的值

messages = {}

changeset =
  Api.UserProduct.changeset(
    user_product,
    %{:voted_vegan => true}
  )

messages =
  case Api.Repo.update(changeset) do
    {:ok, product} ->
      IO.puts("appending messages7")
      Tuple.append(messages, "Product updated")

    {:error, changeset} ->
      IO.puts("appending messages8")
      Tuple.append(messages, "Product not updated")
  end

conn
|> put_resp_content_type("application/json")
|> send_resp(
  200,
  Poison.encode!(%{
    successs: "success",
    errors: Tuple.to_list(messages)
  })
)

我有大约3个案件陈述之后,这是没有问题的。因此,此解决方案不会附加消息。它会覆盖它们。最后一种情况就是messages变量中的所有内容。在每个case语句中添加消息而不是覆盖消息的最佳方式是什么?@beniaminobagins我不理解你的问题。你需要这样的东西吗?让我知道,所以我将其添加到回答中是的,但我需要一个更通用的解决方案。我在添加消息的地方有很深的嵌套代码区域。你的解决方案在那里不起作用。Elixir语言已更新,现在范围不一样。在函数的底部,消息等于{},当它在函数的中间时具有更多的值时。它们必须是不同的范围级别。我只想附加到所有作用域级别的消息,就像一个全局作用域。噩梦