簡體   English   中英

為什么錯誤“ function_clause”,關於erlang,mongodb-erlang,mongodb

[英]Why the error “function_clause”,about erlang,mongodb-erlang,mongodb

對不起,我的池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中嘗試了此代碼

光標= mongo:find(Connection,Collection,Selector),

可以成功運行,並且我已經檢查了mongodb,變量類型,但是最后我不知道如何解決此問題。請幫助我!!:->

問題似乎與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},

或{database,<<“ user” >>},

問候

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM