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.