I’m going through my collection and returning coordinate information, however when it hits an empty record it throws this error:
a=get_coords(doc['coordinates']['coordinates'])
print a
This is the function get_coords:
def get_coords(doc):
if doc == None:
pass
else:
longs, lat = doc
return lat, longs
The error:
TypeError: 'NoneType' object has no attribute '__getitem__'
This then stops my query and no further records are returned.
How can I prevent this from happening, what I mean is I still want it to carry on searching for other records rather than stopping with this error message.
Thanks
Thanks for both answers they both helped. I was able to get it done by:
if doc['coordinates']==None:
pass
else:
b=get_coords(doc['coordinates']['coordinates'])
print b
I do this after first checking that there is indeed a doc in the collection. It now seems to print all existing coordinates in the collection.
Thanks
I’m not sure why you’re printing
b, since that’s not defined in the above code. Maybe you meana?Either way, you should be able to do:
or whatever you want to do with
doc. If somedocitems don’t havecoordinatesyou’ll need to do something similar for that.