Skip to content

Commit 7c9d45e

Browse files
committed
Update example python view code to avoid KeyErrors
If the reader doesn't happen to have the 'date' key in one of the documents the old code would cause couchdb to throw. If the reader happened to be using futon, it would produce a useless 'no response' error. By using .get() you'll always at least get None and this code can be used against any database.
1 parent 361e37d commit 7c9d45e

File tree

1 file changed

+1
-1
lines changed

1 file changed

+1
-1
lines changed

doc/views.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ After restarting CouchDB, the Futon view editor should show ``python`` in
1313
the language pull-down menu. Here's some sample view code to get you started::
1414

1515
def fun(doc):
16-
if doc['date']:
16+
if doc.get('date'):
1717
yield doc['date'], doc
1818

1919
Note that the ``map`` function uses the Python ``yield`` keyword to emit

0 commit comments

Comments
 (0)