为什么错误“function_clause”,关于erlang,mongodb-erlang,mongodb [英] Why the error "function_clause",about erlang,mongodb-erlang,mongodb

查看:168
本文介绍了为什么错误“function_clause”,关于erlang,mongodb-erlang,mongodb的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



运行它后,从shell报告中我确定mongodb-erlang已经为我创建了池。

  ***************************调试********************** *********** 

模块:db_mongo_handler
行:73
数据库连接:< 0.174.0>
***************************调试****************** ***************

模块:db_mongo_handler
行:73
数据库连接:< 0.178.0>
***************************调试****************** ***************

模块:db_mongo_handler
行:73
数据库连接:< 0.180.0>
***************************调试****************** ***************

模块:db_mongo_handler
行:73
数据库连接:< 0.182.0>
***************************调试****************** ***************

模块:db_mongo_handler
行:73
数据库连接:< 0.184.0>
***************************调试****************** ***************

模块:db_mongo_handler
行:73
数据库连接:< 0.186.0>
***************************调试****************** ***************

模块:db_mongo_handler
行:73
数据库连接:< 0.188.0>
***************************调试****************** ***************

模块:db_mongo_handler
行:73
数据库连接:< 0.190.0>
***************************调试****************** ***************

模块:db_mongo_handler
行:73
数据库连接:< 0.192.0>
***************************调试****************** ***************

模块:db_mongo_handler
行:73
数据库连接:< 0.194.0>

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

  get_user(Name,Req) - > 
Collection =<user>>
Selector = {name,Name},
Worker = poolboy:checkout(?DB_Conn_Pool),
io:格式(*********调试******************* **************〜n模块:〜p〜n行:〜p〜n工人:〜p〜n,[?MODULE,?LINE,Worker]),
Request = {get_user,{Collection,Selector}},
UserInfo = gen_server:call(Worker,Request),
io:format(************* **************调试*********************************〜n的模块:〜p〜n行:〜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中的请求/ p>

代码快照:

  handle_call({get_user,{Collection,Selector }},_From,#state {connection = Connection} = State) - > 
io:format(*************************** Debug ************* ********************〜n
模块:〜p〜n行:〜p〜n Handle_call连接:〜p〜n集合:〜p〜 n
选择器:〜p〜n连接是is_pid()?〜p〜n Collection是binary()?〜p〜n,
[?MODULE,?LINE,Connection,Collection,Selector, is_pid(Connection),is_binary(Collection)]),
Cursor = mongo:find(Connection,Collection,Selector),
io:format(************ ***************调试*********************************〜 n模块:〜p〜n行:〜p〜n
光标:〜p〜n,[?MODULE,?LINE,Cursor]),
Result = mc_cursor:rest(Cursor) b $ b [头| _] =结果,
{_,_,_,NameValue,_,AgeValue} =头,
io:format(**************** ***********调试*********************************模块:〜 p〜n行:〜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),

错误报告是:

  *************** ************调试******************************* $ 

模块:db_mongo_handler
行:104
Handle_call连接:< 0.194.0>
集合:<user>>

选择器:{name,<3>>}
连接是is_pid()? true
Collection是binary()? true

= ERROR REPORT ==== 2015年2月4日:: 14:48:39 ===
**一般服务器< 0.194.0>终止
**最后一条消息是{query,false,false,false,false,<user>>,0,0,
{name,< zh-CN,
[]}
**当服务器状态== {state,#Port <0.2666>,
{dict,0,16,16,8 ,80,48,
{[],[],[],[],[],[],[],[],[],[],[],[],[] b $ b [],[],[]},
{{[],[],[],[],[],[],[],[],[],[] ],[],[],
[],[],[]}}},
<>>
{conn_state,unsafe,master }}
**终止原因==
** {function_clause,
[{mongo_protocol,binarize,
[user],
[{file ,src / core / mongo_protocol.erl},{line,108}]},
{mongo_protocol,dbcoll,2,
[{file,src / core / mongo_prot ocol.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}]}
{list,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}]}}}
pre>

我在erlang shell中尝试了这个代码,



Cursor = mongo:find(Connection,Collection ,选择器),



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

解决方

问题似乎与conn_state记录有关,

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

-type database():: binary |原子()。

你有:

  {conn_state,unsafe,master,user} 

一个字符串()不是二进制或原子。



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



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

  {database,user},


{database,<user>>},



注意


Sorry for my pool Englsih!:-) My code is here: http://pastebin.com/zus6dGdz I just want to use the poolboy as my database connection pools,and i use the mongodb-erlang as my driver to communicate with the mongodb.

After running it,from the shell report,I am sure that the mongodb-erlang has created the pools for me

***************************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>

and I use the pools like this: code snap here(in hello_handler.erl,can find it through the the URL above):

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).

And the the worker process will handle the request in the function handle_call/3 in the db_mongo_handler.erl

code snap:

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}.

However,it throw the error from this code:

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

And the error report is :

***************************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}]}]}

I have try this code in erlang shell,the

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

can run successfully,and i have check the mongodb,the variable type,but finally i have no idea how to solve this problem.Please help me !!:->

解决方案

The problem seems to be related to the conn_state record,

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

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

And you have:

{conn_state,unsafe,master,"user"}

where user is a string() not a binary or atom.

The mongo_protocol:binarize/1 function only accepts params as binaries or atoms.

Change in hello_erlang.app.src the database value to:

{database, user},

or {database, <<"user">>},

Regards

这篇关于为什么错误“function_clause”,关于erlang,mongodb-erlang,mongodb的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆