【发布时间】: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.errors 和 q.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