为什么错误" function_clause",关于erlang,mongodb-erlang,mongodb

时间:2015-02-04 06:59:10

标签: mongodb erlang

对不起我的游泳池Englsih!:-) 我的代码在这里: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中尝试了这段代码,

Cursor = mongo:find(连接,集合,选择器),

可以成功运行,我已经检查了mongodb,变量类型,但最后我不知道如何解决这个问题。请帮助我!!: - &gt;

1 个答案:

答案 0 :(得分:1)

问题似乎与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是一个字符串()而不是二进制或原子。

mongo_protocol:binarize / 1函数只接受params作为二进制或原子。

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

{database, user},

或     {database,&lt;&lt;“user”&gt;&gt;},

此致