【发布时间】:2021-01-08 04:59:32
【问题描述】:
我正在尝试从 mongodb 数据库中检索数据,并尝试将该响应传递给前端 reactjs 应用程序。
但我收到此错误,但我找不到它发生的原因:
TypeError:视图函数未返回有效响应。返回类型必须是字符串、字典、元组、响应实例或 WSGI 可调用,但它是一个列表
我需要的是:
我需要检索所有 mongodb 文档并将其传递给前端应用程序,以便我可以使用 reactjs 将其映射到表格中。
这就是我正在尝试的,这给了我错误:
@app.route("/bemployees", methods=["GET"])
def retrieve_all_documents():
client = pymongo.MongoClient(
"<url>"
)
# database
db = client.cdap
# collection (table)
collection = db.predicted_values
documents_ = []
for b in collection.find():
documents_.append({'Age' :b['Age'] ,'DailyRate': b['DailyRate'],
'DistanceFromHome': b['DistanceFromHome'] , 'EnvironmentSatisfaction': b['EnvironmentSatisfaction'],
'HourlyRate':b['HourlyRate'],'JobInvolvement': b['JobInvolvement'],
'JobLevel': b['JobLevel'],'JobSatisfaction' :b['JobSatisfaction'],
'MonthlyIncome': b['MonthlyIncome'], 'MonthlyRate' :b['MonthlyRate'],
'NumCompaniesWorked': b['NumCompaniesWorked'],'PercentSalaryHike' :b['PercentSalaryHike'],
'RelationshipSatisfaction': b['RelationshipSatisfaction'],'StandardHours' :b['StandardHours'],
'TotalWorkingYears': b['TotalWorkingYears'],'TrainingTimesLastYear' :b['TrainingTimesLastYear'],
'YearsAtCompany': b['YearsAtCompany'],'YearsInCurrentRole' :b['YearsInCurrentRole'],
'YearsSinceLastPromotion': b['YearsSinceLastPromotion'],'YearsWithCurrManager' :b['YearsWithCurrManager'],
'MaritalStatus_': b['MaritalStatus_'],'JobRole_' :b['JobRole_'],
'Gender_': b['Gender_'],'EducationField_' :b['EducationField_'],
'Department_': b['Department_'],'BusinessTravel_' :b['BusinessTravel_'],
'OverTime_': b['OverTime_'],'Over18_' :b['Over18_'],
'empName': b['empName'],'empID' :b['empID'],
'PerformanceScore': b['PerformanceScore'],
'id': str(b['_id']) })
return documents_
有人可以帮我解决这个问题吗?
【问题讨论】:
-
尝试返回类似
{'info': documents_}的内容 -
抱歉没听明白?我该怎么做?
-
喜欢
return {'info': documents_}而不是return documents_ -
我仍然遇到同样的错误。为什么会发生任何想法?
-
请使用完整的追溯和您所做的任何编辑更新您的帖子。