couchdb - Passing an argument to map.js in a CouchApp view -


i have couchapp functions social network. have button which, when clicked user, creates , saves couchdb json document fields post_id (the id of post liked), user_id (the id of user liked post), , type (value "like," indicate document like).

alongside post, i'd indicate number of likes has received. map function looks this:

function(doc) {   if (doc.type && doc.type == 'like') {     emit(doc.post_id, 1);   } } 

and reduce, this:

function(post_ids, likes) {   return sum(likes); } 

my problem is, function returns sum of likes in posts in site. i'm thinking reduce.js fine , maybe tweak map.js accept post_id argument can retrieve likes post_id, how do that? post_id comes design document, of course, when "like" button clicked.

thanks.

you need group view parameter. if supply no querystring parameters, default reduce entire contents of view. once apply group=true, can use key or startkey/endkey pick out post id(s) need.

also, there built-in reduce functions can perform basic reduce operations more efficiently. (since compiled erlang code, not javascript source) put _count in place of entire reduce.js. (you can leave off 1 in emit)


Comments

Popular posts from this blog

linux - Using a Cron Job to check if my mod_wsgi / apache server is running and restart -

actionscript 3 - TweenLite does not work with object -

jQuery Ajax Render Fragments OR Whole Page -