为什么报错"function_clause",about erlang,mongodb-erlang,mongodb

Why the error "function_clause",about erlang,mongodb-erlang,mongodb

对不起,我的游泳池英语!:-) 我的代码在这里:http://pastebin.com/zus6dGdz 我只想使用 poolboy 作为我的数据库连接池,我使用 mongodb-erlang 作为我的驱动程序与 mongodb.

进行通信

在 运行 之后,根据 shell 报告,我确信 mongodb-erlang 已经为我创建了池

***************************Debug*********************************

  Module:db_mongo_handler
  Line:73
  Database Connection:<0.174.0>
***************************Debug*********************************

  Module:db_mongo_handler
  Line:73
  Database Connection:<0.178.0>
***************************Debug*********************************

  Module:db_mongo_handler
  Line:73
  Database Connection:<0.180.0>
***************************Debug*********************************

  Module:db_mongo_handler
  Line:73
  Database Connection:<0.182.0>
***************************Debug*********************************

  Module:db_mongo_handler
  Line:73
  Database Connection:<0.184.0>
***************************Debug*********************************

  Module:db_mongo_handler
  Line:73
  Database Connection:<0.186.0>
***************************Debug*********************************

  Module:db_mongo_handler
  Line:73
  Database Connection:<0.188.0>
***************************Debug*********************************

  Module:db_mongo_handler
  Line:73
  Database Connection:<0.190.0>
***************************Debug*********************************

  Module:db_mongo_handler
  Line:73
  Database Connection:<0.192.0>
***************************Debug*********************************

  Module:db_mongo_handler
  Line:73
  Database Connection:<0.194.0>

我这样使用池: 代码快照在这里(在hello_handler.erl中,可以通过上面的URL找到它):

get_user(Name, Req) ->
  Collection = <<"user">>,
  Selector = {name, Name},
  Worker = poolboy:checkout(?DB_Conn_Pool),
  io:format("***************************Debug*********************************~n  Module:~p ~n  Line:~p ~n  Worker:~p ~n", [?MODULE, ?LINE, Worker]),
  Request = {get_user, {Collection, Selector}},
  UserInfo = gen_server:call(Worker, Request),
  io:format("***************************Debug*********************************~n  Module:~p ~n  Line:~p ~n  UserInfo:~p ~n", [?MODULE, ?LINE, UserInfo]),
  cowboy_req:reply(200, [
    {<<"content-type">>, <<"text/plain">>}
  ], UserInfo, Req).

并且工作进程将在 db_mongo_handler.erl

中的函数 handle_call/3 中处理请求

代码快照:

handle_call({get_user, {Collection, Selector}}, _From, #state{connection = Connection} = State) ->
  io:format("***************************Debug*********************************~n
  Module:~p ~n  Line:~p ~n   Handle_call Connection:~p ~n Collection:~p ~n
  Selector:~p ~n Connection is is_pid()? ~p ~n Collection is binary()? ~p ~n",
    [?MODULE, ?LINE, Connection, Collection, Selector, is_pid(Connection), is_binary(Collection)]),
  Cursor = mongo:find(Connection, Collection, Selector),
  io:format("***************************Debug*********************************~n Module:~p ~n  Line:~p ~n
  Cursor:~p ~n", [?MODULE, ?LINE, Cursor]),
  Result = mc_cursor:rest(Cursor),
  [Head | _] = Result,
  {_, _, _, NameValue, _, AgeValue} = Head,
  io:format("***************************Debug*********************************~n Module:~p ~n  Line:~p ~n
  NameValue:~p ~n  AgeValue:~p ~n ", [?MODULE, ?LINE, NameValue, AgeValue]),
  BackData = binary_to_list(<<NameValue/bits, <<":">>/bits, AgeValue/bits>>),
  {reply, BackData, State}.

但是,它从这段代码中抛出错误:

Cursor = mongo:find(Connection, Collection, Selector),

错误报告是:

***************************Debug*********************************

  Module:db_mongo_handler
  Line:104
   Handle_call Connection:<0.194.0>
 Collection:<<"user">>

  Selector:{name,<<"three">>}
 Connection is is_pid()? true
 Collection is binary()? true

=ERROR REPORT==== 4-Feb-2015::14:48:39 ===
** Generic server <0.194.0> terminating
** Last message in was {query,false,false,false,false,<<"user">>,0,0,
                              {name,<<"zhk">>},
                              []}
** When Server state == {state,#Port<0.2666>,
                               {dict,0,16,16,8,80,48,
                                     {[],[],[],[],[],[],[],[],[],[],[],[],[],
                                      [],[],[]},
                                     {{[],[],[],[],[],[],[],[],[],[],[],[],[],
                                       [],[],[]}}},
                               <<>>,
                               {conn_state,unsafe,master,"user"}}
** Reason for termination ==
** {function_clause,
       [{mongo_protocol,binarize,
            ["user"],
            [{file,"src/core/mongo_protocol.erl"},{line,108}]},
        {mongo_protocol,dbcoll,2,
            [{file,"src/core/mongo_protocol.erl"},{line,44}]},
        {mongo_protocol,put_message,3,
            [{file,"src/core/mongo_protocol.erl"},{line,74}]},
        {mc_worker_logic,'-encode_requests/2-fun-0-',3,
            [{file,"src/connection/mc_worker_logic.erl"},{line,22}]},
        {lists,foldl,3,[{file,"lists.erl"},{line,1261}]},
        {mc_worker_logic,make_request,3,
            [{file,"src/connection/mc_worker_logic.erl"},{line,73}]},
        {mc_worker,handle_call,3,
            [{file,"src/connection/mc_worker.erl"},{line,75}]},
        {gen_server,try_handle_call,4,[{file,"gen_server.erl"},{line,607}]}]}

我已经在 erlang shell 中尝试过这段代码,

光标 = mongo:find(连接、集合、选择器),

可以运行成功,我已经检查了mongodb变量类型,但最后我不知道如何解决这个problem.Please帮助我!!:->

问题似乎与 conn_state 记录有关,

-record(conn_state, {
    write_mode = unsafe :: write_mode(),
    read_mode = master :: read_mode(),
    database :: database()
}).

-type database() :: binary | atom().

你有:

{conn_state,unsafe,master,"user"}

其中 user 是一个 string() 而不是二进制或原子。

mongo_protocol:binarize/1 函数只接受二进制或原子形式的参数。

将 hello_erlang.app.src 数据库值更改为:

{database, user},

或 {数据库, <<"user">>},

此致