Using Nano to query a Couch view in NodeJS, to count the occurrences of tags in all documents. If I choose reduce in Fauxton on the view, it correctly displays with each tag name as the key, and the count. However, from my Node app, I just get 'Null' as the key, and a total of all tags.
The tag name (rather than the _id) is stored in each document. This is my map function (using the _count reduce function):
function (doc, meta) {
if(doc.tags) {
for(var i in doc.tags) {
emit(doc.tags[i],1);
}
}
}
This is my (Nano) code in Node:
app.get('/listtags', function(req, res) {
couch.view('objects', 'tags-count', {reduce: true}, function(err, body) {
console.log(body);
});
});
And it returns:
{ rows: [ { key: null, value: 8 } ] }
EDIT Adding example docs:
Tag:
{
"_id": "...",
"_rev": "1-...",
"type": "tag",
"name": "petrol"
}
Doc containing tags:
{
"_id": "...",
"_rev": "3-...",
"type": "car",
"title": "Audi",
"desc": "description here",
"tags": [
"petrol",
"aircon",
"leather seats"
]
}