When I try to test my /articles route with the postal code 02138 (or any other code for that matter) I get something along the lines of this:
KeyError: '02138' at line 43:
cache = lookup(str(coordinates))
articles function:
@app.route("/articles")
def articles():
"""Look up articles for geo."""
# cannot use try here unfortunately
if not request.args.get("geo"):
raise RuntimeError("invalid lat/lng")
coordinates = request.args.get("geo")
# now use lookup (in helpers.py) to return newsfeed
cache = lookup(str(coordinates))
return jsonify([cache])
If I didn't know better, I would have thought the error would be with jsonify since I don't know if I entirely understand how it works (I found flask's documentation to be a little cryptic on it), but instead it is this line.
The reason I cast coordinates to a string was to make sure that they are not numeric as per dino's answer here:
pset8 addMarker can't get articles
EDIT
I changed the last line of my code to be
return jsonify(cache)
Based on some answers I read from Dino, but since the problem is with the line previous, it hasn't really fixed anything.
Here is the traceback error:
Traceback (most recent call last):
File "/usr/local/lib/python3.4/dist-packages/flask/app.py", line 1994, in
__call__
return self.wsgi_app(environ, start_response)
File "/usr/local/lib/python3.4/dist-packages/flask/app.py", line 1985, in
wsgi_app
response = self.handle_exception(e)
File "/usr/local/lib/python3.4/dist-packages/flask/app.py", line 1540, in
handle_exception
reraise(exc_type, exc_value, tb)
File "/usr/local/lib/python3.4/dist-packages/flask/_compat.py", line 33,
in reraise
raise value
File "/usr/local/lib/python3.4/dist-packages/flask/app.py", line 1982, in
wsgi_app
response = self.full_dispatch_request()
File "/usr/local/lib/python3.4/dist-packages/flask/app.py", line 1614, in
full_dispatch_request
rv = self.handle_user_exception(e)
File "/usr/local/lib/python3.4/dist-packages/flask/app.py", line 1517, in
handle_user_exception
reraise(exc_type, exc_value, tb)
File "/usr/local/lib/python3.4/dist-packages/flask/_compat.py", line 33,
in reraise
raise value
File "/usr/local/lib/python3.4/dist-packages/flask/app.py", line 1612, in
full_dispatch_request
rv = self.dispatch_request()
File "/usr/local/lib/python3.4/dist-packages/flask/app.py", line 1598, in
dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)
File "/home/ubuntu/workspace/pset8/mashup/application.py", line 43, in
articles
cache = lookup(str(coordinates))
File "/home/ubuntu/workspace/pset8/mashup/helpers.py", line 9, in lookup
return lookup.cache[geo]
KeyError: '02138'
https://ide50-username.cs50.io/articles?geo=02138
in the browser (your username of course), you get a KeyError?Traceback
at the top of the error it will give aCopy/Paste friendly
version. )?geo=London
orgeo=NJ
?geo
in lookup.cache line 8if geo in lookup.cache:
and then NOT "find" if on line 9?....Aha, found it! Answer forthcoming.