如何在 Phoenix 中处理来自 Rethinkdb 的 success/error flash 消息

How to handle success/error flash messages from Rethinkdb in Phoenix

我正在尝试通过 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),每次都报错。 我正在测试的错误是数据库 table 是否不存在(我将 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)。 我需要什么才能获得该字段?

使用 q.data["errors"]) 而不是 q.data.errors

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