【问题标题】:How to handle success/error flash messages from Rethinkdb in Phoenix如何处理来自凤凰城 Rethinkdb 的成功/错误闪现消息
【发布时间】:2015-07-29 21:22:34
【问题描述】:

我正在尝试通过 Flash 消息在 Phoenix 框架中查询到我的 RethinkDB 的成功或失败。但是,我不知道在我的代码块中从 RethinkDB 检索结果消息的正确签名。

def index(conn, _params) do

  #Query that creates a table in the database
    table_create("contentText")
    |> Basedados.Database.run
      # List all elements of a table from the database
    q = table("contentText")
      |> Basedados.Database.run #Run the query through the database
      |> IO.inspect
    conn
    |> put_flash(:error, "Some Message")
    |> put_flash(:info, "Another Message")
    |> render "index.html", contentText: q #Render users searched on the users template
  end

编辑:好的,我发现应该有一个错误字段:

%RethinkDB.Record{data: %{"deleted" => 0, "errors" => 0, "generated_keys" => ["15cc4e19-fc72-4c19-b3a5-47141b6a63e0"], "inserted" => 1, "replaced" => 0, "skipped" => 0, "unchanged" => 0}}

但是我似乎没有它(我尝试过像 q.errorsq.data.errors 一样)并且每次都会出错。 我正在测试的错误是数据库表是否不存在(我将 contentText 完全更改为其他内容)。

我可以对数据进行一些错误检查:

q = table("contentText")
  |> Basedados.Database.run #Run the query through the database

if is_nil(q.data) do
  conn
  |> put_flash(:error, "Error")
  |> render "index.html", contentText: "failed" #Render users searched on the users template
else
  conn
  |> put_flash(:info, "Sucess")
  |> render "index.html", contentText: q #Render users searched on the users template
end

但这似乎是一个有限的解决方案,因为它只检测消息中是否有数据。错误可能完全是另外一回事。而且由于该字段似乎计算了它们,我想获得该值并在我的条件下使用它(如果错误> 0)。 我需要什么才能获得该领域?

【问题讨论】:

    标签: elixir rethinkdb flash-message phoenix-framework


    【解决方案1】:

    使用q.data["errors"]) 代替q.data.errors

    根据问题hamiltop/rethinkdb-elixir#59,可以在失败时使用q.data["r"]

    【讨论】:

    • 这似乎确实解决了这部分问题。但我仍然不知道如何将其链接到 Flash 消息。
    • 没有。我的意思是我想在客户端触发带有错误消息的闪存消息。我知道如何在客户端执行它们,但我不知道如何从我的频道发送触发器。
    • 根据Phoenix documentation 你做对了。如果要发送错误 RethinDB,只需将 put_flash(:error, "Error") 更改为 put_flash(:error, Enum.join(q.data["r"], "\n"))
    猜你喜欢
    • 2016-05-03
    • 1970-01-01
    • 1970-01-01
    • 2020-06-11
    • 2016-06-24
    • 1970-01-01
    • 2018-11-27
    • 2017-07-21
    • 1970-01-01
    相关资源
    最近更新 更多