incubator-couchdb-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Thomas Van de Velde <thomase...@gmail.com>
Subject [Erlang] Returning JSON from a List
Date Fri, 07 Oct 2011 15:49:49 GMT
Hi,

I made great progress converting Javascript shows to Erlang. I need some
help with lists in Erlang. I have a JSON document that has pointers to other
documents (with include_docs=true). Within the list, I construct a new JSON
document that aggregates information I pulled from the documents that were
referenced. Below you'll find the Javascript implementation. I am having
trouble reproducing this in Erlang.

This is what I have in Javascript:

function(head, req) {

start({
"headers" : {
"Content-Type" : "text/plain"
}
});

var page = [];

while( row = getRow()) {
var module = row.value.module;
var desc = row.doc.desc;
var title = row.doc.title;
 var module = {
"module" : module,
"desc" : desc,
"title" : title
};
page.push(module);
}
send(JSON.stringify(page));

}

I am starting from what's in the unit test:

%% Page generator (from Futon unit test)
fun(Head, {Req}) ->
Send(<<"head">>),
Fun = fun({Row}, _) ->
Val = couch_util:get_value(<<"value">>, Row, -1),
Send(list_to_binary(integer_to_list(Val))),
{ok, nil}
end,
{ok, _} = FoldRows(Fun, nil),
<<"tail">>
end.

In a show I was able to construct a new JSON document and then return it
with:

{[{<<"code">>, 200}, {<<"headers">>, {[]}}, {<<"json">>,
MyDoc}]}

How can I do something similar where I iterate through the rows, take some
data from each row and then send the result as a single JSON document?

Thank you!

Thomas

Mime
  • Unnamed multipart/alternative (inline, None, 0 bytes)
View raw message