我正在尝试使用erlang语言在CouchDB中创建一个视图文档。我已在配置中启用了erlang query_server。在创建文档时,该语言已设置为javascript,如果我转到设计文档并将其更改为erlang,则会从CouchDB中收到错误消息。
fun({Doc}) ->
case proplists:get_value(<<"type">>, Doc) of
undefined ->
ok;
Type ->
Identifiers = proplists:get_value(<<"institution">>, Doc),
lists:foreach(fun(Doc)
->
Emit(doc._id,Doc);
_ ->
ok
end.
发现的错误是:
Error running query. Reason: ({badmatch,[<<"error">>, {9,erl_parse,["syntax error before: ","'.'"]}, {9,erl_parse,["syntax error before: ","'.'"]}]}) [{couch_query_servers,'-start_doc_map/3-fun-0-',2, [{file,"src/couch_query_servers.erl"},{line,62}]}, {lists,foreach,2,[{file,"lists.erl"},{line,1337}]}, {couch_query_servers,start_doc_map,3, [{file,"src/couch_query_servers.erl"},{line,61}]}, {couch_mrview_updater,start_query_server,1, [{file,"src/couch_mrview_updater.erl"},{line,231}]}, {couch_mrview_updater,map_docs,2, [{file,"src/couch_mrview_updater.erl"},{line,184}]}]
答案 0 :(得分:5)
对于fun
传递给lists:foreach
的情况,您缺少右括号,end
的{{1}}和{{1} }(用于封闭case
。同样,end
将要操作的列表作为第二个参数。试试这个:
fun
我不确定lists:foreach
。由于fun({Doc}) ->
case proplists:get_value(<<"type">>, Doc) of
undefined ->
ok;
Type ->
Identifiers = proplists:get_value(<<"institution">>, Doc),
lists:foreach(fun(Doc) ->
Emit(doc._id,Doc);
_ ->
ok
end,
Identifiers) % ← add argument and closing parenthesis here
end % ← add 'end' for 'case' here
end. % ← add 'end' for 'fun' here
以大写字母开头,因此它是指变量,但是这里没有Emit(doc._id,Doc)
变量-还是CouchDB使该变量可用? Emit
也不是有效的Erlang语法;我猜应该是Emit
。