【发布时间】:2021-08-17 02:15:20
【问题描述】:
我希望能够查询我的 mongo 数据库并获取我的 stocksCollection 集合中所有条目的结果。我正在使用allStocks = list(stocksCollection.find({})),它为我提供了该集合中所有条目的列表。但是,当我尝试将此作为对 get 请求的响应返回时,我收到错误:
TypeError: The view function did not return a valid response.
The return type must be a string, dict, tuple, Response instance, or WSGI callable, but it was a list.
这是我用来得到这个的简单代码:
@app.route("/stocks", methods=['GET'])
def getAllStocks():
return list(stocksCollection.find({}))
我也试过:return stocksCollection.find({})(错误,因为它是一种类型:游标)和
allStocks = list(stocksCollection.find({}))
return {'data': allStocks}
但这只是给了我错误TypeError: Object of type ObjectId is not JSON serializable。关于我可以将此光标更改为何种格式的任何想法,以便我能够将其返回到我的 api 调用(这不会直接提供给网页,只是被前端调用以进行一些计算)
【问题讨论】:
标签: python json mongodb flask cursor