非常新的python并尝试查询mongo db。
我正在学习本教程(https://blog.miguelgrinberg.com/post/designing-a-restful-api-with-python-and-flask),并且我安装了一些mongodb样本餐厅数据。
@app.route('/todo/api/v1.0/tasks', methods=['GET'])
def get_tasks():
cursor = db.find({"borough": "Bronx"})
result = []
for restaurant in cursor:
result.append(restaurant)
return result
编辑**
Traceback (most recent call last):
File "/home/ciprian/dev/todo-api/flask/lib/python2.7/site-packages/flask/app.py", line 1997, in __call__
return self.wsgi_app(environ, start_response)
File "/home/ciprian/dev/todo-api/flask/lib/python2.7/site-packages/flask/app.py", line 1985, in wsgi_app
response = self.handle_exception(e)
File "/home/ciprian/dev/todo-api/flask/lib/python2.7/site-packages/flask/app.py", line 1540, in handle_exception
reraise(exc_type, exc_value, tb)
File "/home/ciprian/dev/todo-api/flask/lib/python2.7/site-packages/flask/app.py", line 1982, in wsgi_app
response = self.full_dispatch_request()
File "/home/ciprian/dev/todo-api/flask/lib/python2.7/site-packages/flask/app.py", line 1615, in full_dispatch_request
return self.finalize_request(rv)
File "/home/ciprian/dev/todo-api/flask/lib/python2.7/site-packages/flask/app.py", line 1630, in finalize_request
response = self.make_response(rv)
File "/home/ciprian/dev/todo-api/flask/lib/python2.7/site-packages/flask/app.py", line 1740, in make_response
rv = self.response_class.force_type(rv, request.environ)
File "/home/ciprian/dev/todo-api/flask/lib/python2.7/site-packages/werkzeug/wrappers.py", line 885, in force_type
response = BaseResponse(*_run_wsgi_app(response, environ))
File "/home/ciprian/dev/todo-api/flask/lib/python2.7/site-packages/werkzeug/wrappers.py", line 57, in _run_wsgi_app
return _run_wsgi_app(*args)
File "/home/ciprian/dev/todo-api/flask/lib/python2.7/site-packages/werkzeug/test.py", line 884, in run_wsgi_app
app_rv = app(environ, start_response)
TypeError: 'list' object is not callable
似乎对我有用的解决方案:
from bson.json_util import dumps
@app.route('/todo/api/v1.0/tasks', methods=['GET'])
def get_tasks():
cursor = db.find({"borough": "Bronx"})
result = []
for restaurant in cursor:
result.append(restaurant)
return dumps(result)