在CouchDB映射函数中获取GET请求

时间:2015-04-20 12:05:52

标签: javascript mapreduce couchdb

是否可以在视图的map函数中使用GET请求向外部Web服务发出GET请求?例如。我有一个REST API分类服务。我想通过调用(非异步)API函数在map函数中对文档进行分类。是否可以在JavaScript中实现?

1 个答案:

答案 0 :(得分:2)

不使用默认的javascript查询服务器。但是如果启用native erlang query server,您可以执行任意代码,包括发出http请求。例如,你可以这样做

%% Map Function
 fun({Doc}) ->
     <<K,_/binary>> = proplists:get_value(<<"_rev">>, Doc, null),
     V = proplists:get_value(<<"_id">>, Doc, null),

     %% Making the http request

     {ok,{Status,Headers,Body}} = httpc:request("some url"),

     %% do some stuff with the response and then emit

     Emit(<<K>>, V)

end.