couchdb-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Paul Davis" <paul.joseph.da...@gmail.com>
Subject Re: Reduce function error "keys has no properties"
Date Sat, 08 Nov 2008 01:17:38 GMT
John,

You're not accounting for a rereduce. There's a third parameter
`rereduce` for reduce functions that is a boolean. Fairly certain when
it's true keys is null. This is the commutative and associative thing
rearing. Your reduce function needs to operate on its own output.

Paul

On Fri, Nov 7, 2008 at 8:05 PM, John Bartak <john.bartak@autodesk.com> wrote:
> I get the error "Query Server Log Message: function raised exception (TypeError: keys
has no properties)" when I run my reduce function and try to use the keys parameter.  I thought
keys was supposed to have the documentId's for each document passed in through the values
parameter?
>
>  My View looks like:
>
> function(doc)
> {
>   function emitParts(parts,doc)
>   {
>           for (var i = 0; i < parts.length; ++i)
>           {
>                     emit(parts[i].substr(0,3).toLowerCase(),doc);
>           }
>   }
>
>   if (doc.type == "Person")
>   {
>                  var parts;
>                  if (doc.Name && typeof(doc.Name) == "string")
>                  {
>                                parts = doc.Name.split(new RegExp("[ ]"));
>                                emitParts(parts,doc);
>                  }
>                  if (doc.Email && typeof(doc.Email) == "string")
>                  {
>                                parts = doc.Email.split(new RegExp("[.@ ]"));
>                                emitParts(parts,doc);
>                  }
>
>   }
>  }
>  function( keys, values )
>  {
>               var usedKeys = '';
>                var reduced = [];
>                for ( var i = 0; i < keys.length; ++i )
>                {
>                                if (usedKeys.indexOf('%!' + keys[i] + '%!') == -1)
>                                {
>                                                usedKeys = usedKeys + '%!' + keys[i] +
'%!';
>                                                reduced.push(values[i]);
>                                }
>                }
>                return reduced;
> }
>
> If I remove any reference to keys from the reduce function, the error goes away.
>
> This View creates keys where that are the first 3 characters found in every word of the
Person's eMail and Name fields.
>
> The intent of the usedKeys code in the reduce function is deal with the case where a
document has an Email field and a Name field that have words that start with the same 3 characters.
 It prevents the same document from being inserted in the results twice.
>
>
>

Mime
View raw message