SImple reduce function is not working, what is wrong with my reduce function code in couchDB?

579 views Asked by At

i am new to couchDB. I started with simple map/reduce. i dont know why i am not able to receive proper values for the piece of code i attached. thanks in advance !!!

My mapping code as follows:

function(doc){ 
if(doc.release.formats.format.descriptions.description == 'Album'){
doc.release.artists.artist.forEach(function(i){emit(i.name,doc.release.title)})
}
}

My resultant map values key: "Trevor loveys" value:"Era"

My reduce code

 function(keys,values,rereduce)
{
 for(var i=0; i<keys.length();i++)
{
 if(keys[i] == "Trevor Loveys")
 {
  return values;
}
}

Once i have reduced, i am getting answer like

Key:"Trevor loveys" value: null

Update. A sample of my json data is provided below:

{
"release": {
    "genres": {
        "genre": "Electronic"
    },
    "status": "Accepted",
    "videos": {
        "video": [
            {
                "title": "Reflections- 2nd Nature",
                "duration": 436,
                "description": "Reflections- 2nd Nature",
                "src": "http://www.youtube.com/watch?v=w4c8Xi56YZ8",
                "embed": true
            },
            {
                "title": "Trevor Loveys - Bra",
                "duration": 414,
                "description": "Trevor Loveys - Bra",
                "src": "http://www.youtube.com/watch?v=DcakkW0uZcs",
                "embed": true
            },
            {
                "title": "Trevor Loveys - We can make it",
                "duration": 463,
                "description": "Trevor Loveys - We can make it",
                "src": "http://www.youtube.com/watch?v=lJN7Wpl6HrE",
                "embed": true
            },
            {
                "title": "Trevor Loveys Forever After (ERA).mp4",
                "duration": 311,
                "description": "Trevor Loveys Forever After (ERA).mp4",
                "src": "http://www.youtube.com/watch?v=00qmrYHgm54",
                "embed": true
            }
        ]
    },
    "labels": {
        "label": {
            "catno": "ALO CD003",
            "name": "Alola"
        }
    },
    "companies": "",
    "styles": {
        "style": [
            "Deep House",
            "Downtempo"
        ]
    },
    "formats": {
        "format": {
            "text": "",
            "name": "CD",
            "qty": 1,
            "descriptions": {
                "description": "Album"
            }
        }
    },
    "country": "UK",
    "id": 1504,
    "released": "1999-00-00",
    "artists": {
        "artist": [
            {
                "id": 2049,
                "anv": "",
                "name": "Trevor Loveys",
                "role": "",
                "tracks": "",
                "join": "Presents"
            },
            {
                "id": 8637,
                "anv": "2nd Nature",
                "name": "Second Nature",
                "role": "",
                "tracks": "",
                "join": ""
            }
        ]
    },
    "title": "Era",
    "master_id": 11157,
    "tracklist": {
        "track": [
            {
                "position": 1,
                "duration": "",
                "title": "Forever After"
            },
            {
                "position": 2,
                "duration": "",
                "title": "Wisdom"
            },
            {
                "position": 3,
                "duration": "",
                "title": "Look To The Sky"
            },
            {
                "position": 4,
                "duration": "",
                "title": "Falling Down"
            },
            {
                "position": 5,
                "duration": "",
                "title": "Life Stories"
            },
            {
                "position": 6,
                "duration": "",
                "title": "Rainy Afternoon"
            },
            {
                "position": 7,
                "duration": "",
                "title": "Reflections"
            },
            {
                "position": 8,
                "duration": "",
                "title": "Got To Go There"
            },
            {
                "position": 9,
                "duration": "",
                "title": "We Can Make It"
            },
            {
                "position": 10,
                "duration": "",
                "title": "No Compromise"
            },
            {
                "position": 11,
                "duration": "",
                "title": "Era"
            },
            {
                "position": 12,
                "duration": "",
                "title": "City 2 City"
            },
            {
                "position": 13,
                "duration": "",
                "title": "Depth Charge"
            }
        ]
    },
    "data_quality": "Correct",
    "extraartists": {
        "artist": [
            {
                "id": 49433,
                "anv": "",
                "name": "Chris Pedley",
                "role": "Written-By, Producer",
                "tracks": "",
                "join": ""
            },
            {
                "id": 2049,
                "anv": "",
                "name": "Trevor Loveys",
                "role": "Written-By, Producer",
                "tracks": "",
                "join": ""
            }
        ]
    }
}
}

My intention is to get albums names by the artist "Trevor Loveys"

I used the above method to achieve my aim. however, I am unable to understand why it fails.

1

There are 1 answers

3
Akshat Jiwan Sharma On BEST ANSWER

key in the reduce function is an array like:

[["some_key","89a85cad58c426cd85a55bac8e0350f8"]]

You can test it by using log(key) in your reduce function.

So your if block fails and no reduce is returned.