Skip to content
This repository has been archived by the owner on Aug 30, 2024. It is now read-only.

KeyError: 'id' on iterating over grouped view #456

Closed
beadsland opened this issue Jan 19, 2020 · 2 comments · Fixed by #487
Closed

KeyError: 'id' on iterating over grouped view #456

beadsland opened this issue Jan 19, 2020 · 2 comments · Fixed by #487
Assignees
Labels
Milestone

Comments

@beadsland
Copy link

Bug Description

1. Steps to reproduce and the simplest code sample possible to demonstrate the issue

for item in db.get_view_result(ddoc_id, view_name, group=True): print item

2. What you expected to happen

Iterate over all grouped reductions.

3. What actually happened

Iterate over grouped reductions, followed by:

File "/usr/local/lib/python2.7/dist-packages/cloudant/result.py", line 400, in _iterator
if last['id']:
KeyError: 'id'

Environment details

  • Version(s) that are affected by this issue.

    2.12.0

  • Python version

    2.7.17

4. Simple fix

Change the offending line to:

if 'id' in last:

@bessbd
Copy link
Contributor

bessbd commented Jan 20, 2020

Hi @beadsland ,

Thank you for this bug report. Can you please provide a more complete steps to reproduce? I'm asking, because the following works for me:

from cloudant.client import CouchDB
client = CouchDB("admin", "password", url='http://127.0.0.1:5984', connect=True)

session = client.session()
db = client['testing']

for item in db.get_view_result("newDD", "new-view", group=True):
    print(item)

client.disconnect()

where

curl http://admin:[email protected]:5984/testing/_all_docs?include_docs=true |jq
{
  "total_rows": 2,
  "offset": 0,
  "rows": [
    {
      "id": "_design/newDD",
      "key": "_design/newDD",
      "value": {
        "rev": "2-0611118a25d6d8435680e48fdefe1f37"
      },
      "doc": {
        "_id": "_design/newDD",
        "_rev": "2-0611118a25d6d8435680e48fdefe1f37",
        "views": {
          "new-view": {
            "map": "function (doc) {\n  emit(doc._id, 1);\n}",
            "reduce": "_count"
          }
        },
        "language": "javascript"
      }
    },
    {
      "id": "f4e47445a7324cb60c32f5d41e00077a",
      "key": "f4e47445a7324cb60c32f5d41e00077a",
      "value": {
        "rev": "1-4c6114c65e295552ab1019e2b046b10e"
      },
      "doc": {
        "_id": "f4e47445a7324cb60c32f5d41e00077a",
        "_rev": "1-4c6114c65e295552ab1019e2b046b10e",
        "foo": "bar"
      }
    }
  ]
}

@emlaver
Copy link
Member

emlaver commented Nov 20, 2020

This issue can be reproduced when the amount of documents is greater than the page size.
Example using the code block above:

...
for item in db.get_view_result("views101", "diet_count", group=True, page_size=1):
    print(item)

I'll open a PR to check that the id exists on line 400 in cloudant/result.py.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants