[ https://issues.apache.org/jira/browse/COUCHDB-1506?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=13402939#comment-13402939 ]
James Howe commented on COUCHDB-1506: ------------------------------------- There's no guarantee over what values are going to be passed to your reduce function: they get split up according to how the btree is arranged. Therefore, there are going to be occasions where none of the values are of type 'customer', and your reduce is going to return null. I suggest you use one view to do the reduce (emitting only purchase and refill documents), and then you can just get the customer documents by their id in a separate call. > View returns null value after adding a document > ----------------------------------------------- > > Key: COUCHDB-1506 > URL: https://issues.apache.org/jira/browse/COUCHDB-1506 > Project: CouchDB > Issue Type: Bug > Components: JavaScript View Server > Affects Versions: 1.2 > Environment: Windows XP SP3 > Reporter: Marcin Stefaniuk > > I have three types of documents: customer, refill and purchase. I defined a > view that aggregates customer data with current balance (from refill / > purchase values). Values customer._id and purchase/refill.customer are > joining keys. Map function looks as follows: > function(doc) { > if (doc.type=='purchase' || doc.type=='refill') { > emit(doc.customer, {value: doc.value, type: doc.type}); > } else if (doc.type=='customer') { > emit(doc._id, doc); > } > } > Reduce function is: > function(keys, values, rereduce) { > var customer = null, sum = 0; > values.forEach(function(element) { > if (element.type=='purchase') { > sum -= element.value; > } else if (element.type=='refill') { > sum += element.value; > } else if (customer==null && element.type=='customer') { > customer = element; > } > }); > customer.credit = sum; > return customer; > } > This view is working with success but after several adding of purchase > documents it returns null as value. -- This message is automatically generated by JIRA. If you think it was sent incorrectly, please contact your JIRA administrators: https://issues.apache.org/jira/secure/ContactAdministrators!default.jspa For more information on JIRA, see: http://www.atlassian.com/software/jira