0
這簡單的代碼從一個更大的片降低生成關於pymongo「發現」錯誤撥打:Flask_PyMongo:類型錯誤:「數據庫」對象不是可調用
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)
,如果我用蟒運行此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瓶== 0.12燒瓶PyMongo == 0.4.1
這裏發生了什麼?