:couchbeam.save_doc/2 在 Elixir 中

:couchbeam.save_doc/2 in Elixir

Elixir using Couchbeam 中编写了一些代码以将文档保存到 CouchDB:

case :couchbeam.open_or_create_db(server, "database", []) do
  { :ok, db } ->
    doc = [
      _id: "auth_#{username}",
      content: 'some text'
    ]

    case :couchbeam.save_doc(db, doc) do
      { :ok, saved_doc } -> saved_doc
      { :error, :econnrefused } -> %{
        :error_code => "save_error",
        :error => "Failure to create account"
      }
    end
  { :error, :econnrefused } ->
    IO.puts "Could not connect to server"
end

这会导致错误,指出 save_doc/4 不是匹配的函数子句,而在我的本地 couchbeam.erl.

save_doc/4定义为:

-spec save_doc(Db::db(), doc(), mp_attachments(), Options::list()) ->
    {ok, doc()} | {error, term()}.
save_doc(#db{server=Server, options=Opts}=Db, {Props}=Doc, Atts, Options) ->

说明未找到函数子句的堆栈跟踪:

 ** (FunctionClauseError) no function clause matching in :couchbeam.save_doc/4
 stacktrace:
   (couchbeam) src/couchbeam.erl:560: :couchbeam.save_doc({:db, {:server, "http://localhost:5984", [basic_auth: {"admin", "admin"}]}, "database", [basic_auth: {"admin", "admin"}]}, [_id: "auth_hello", content: 'some text'], [], [])
   (cs) lib/models/Auth.ex:60: anonymous fn/3 in Server.Model.Auth.signup/3
   test/model/auth_test.exs:7

save_doc/2 是否需要传入的参数缺少的内容?

应该是

doc = {[ { "_id", "auth_#{username}" }, { "content", "some text" } ]}

类型doc()在Erlang中被定义为{[{ejson_key(), ejson_term()}]}。您使用的关键字列表文字语法需要包装在一个元组中。