incubator-couchdb-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Tom Sante <>
Subject Re: Reduce function results
Date Fri, 21 May 2010 21:19:35 GMT
On 21/05/10 19:53, J Chris Anderson wrote:
> On May 21, 2010, at 10:37 AM, Tom Sante wrote:
>> >  Because the data was stored like that, but indeed for future database this
will be stored as a JSON number. Still doesn't explain why the different behaviour of Number()
in couch compared to 'js'.
>> >
> The only factor I can think of is the inclusion of json2.js in the main.js source code...
You might have a look through share/server for clues.

You're right I think this part of json2.js might be it:

// Otherwise, iterate through all of the keys in the object.

                 for (k in value) {
                     if (, k)) {
                         v = str(k, value);
                         if (v) {
                             partial.push(quote(k) + (gap ? ': ' : ':') 
+ v);

For a Number is seen as an typeof = object it will iterate through it 
but that doesn't return anything for a Number() object so it ends up as 
being {} in JSON.
The v from var v = new Number(values[val][v_n]) only directly gets 
converted to JSON if there was only one doc send to my reduce function. 
If there was more than one doc send to the reduce function it works 
because the v is send to the Math.min() function which will always 
return a typeof number, and that does get converted to JSON correctly.

Calling v.valueOf() returns the Number object as a typeof number and 
solves the problem.

View raw message