这段简单的代码从一个更大的部分缩小,在pymongo'find'调用中产生错误:
from flask import Flask, request, jsonify
from flask_pymongo import PyMongo
from os import environ
CLIENT_PATH = "/api/v1.0"
app = Flask(__name__)
app.config['MONGO_URI'] = environ.get('MONGODB_URI', "mongodb://localhost:27017/test")
mongo = PyMongo(app)
@app.route(CLIENT_PATH, methods=['GET'])
def getClients():
data = []
cursor = mongo.db.client.find({}, {"_id": 0})
for item in cursor:
client_detail = {}
client_detail['url'] = CLIENT_PATH + "/" + str(item.get('id'))
client_detail['clientTitle'] = str(item.get('firstName')) + " " + str(item.get("familyName"))
data.append(client_detail)
return jsonify({"status": "success", "payload": data})
if __name__ == "__main__":
app.run(debug=True)
如果我使用python 2.7运行它,并在“http://localhost:5000/api/v1.0”上从浏览器拨打电话,我收到以下错误消息:
File "/Users/markschulz/Projects/client/flask-client/app.py", line 16, in getClients
cursor = mongo.db.client.find({}, {"_id": 0})
File "/Users/markschulz/Projects/client/venv/lib/python2.7/site-packages/pymongo/database.py", line 1116, in __call__
self.__name, self.__client.__class__.__name__))
TypeError: 'Database' object is not callable. If you meant to call the 'find' method on a 'MongoClient' object it is failing because no such method exists.
正在使用的软件版本是:pymongo == 3.4.0 Flask == 0.12 Flask-PyMongo == 0.4.1
这里有什么?
答案 0 :(得分:0)
你想要这样的东西:
mongo.db.collection.find({}, {"_id": 0})
其中“db”是数据库的名称,“collection”是集合的名称。