[英]Flask is returning TypeError: The view function did not return a valid response
[英]How to solve the TypeError: The view function did not return a valid response in python flask api
我正在嘗試從mongodb
數據庫中檢索數據並嘗試將該響應傳遞給前端reactjs
應用程序。
但是我收到這個錯誤,我找不到它發生的原因:
類型錯誤:視圖函數未返回有效響應。 返回類型必須是字符串、字典、元組、響應實例或可調用的 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_
有人可以幫我解決這個問題嗎?
Flask 視圖不能返回列表。 解決此問題的一種方法是將您的列表轉換為 JSON 字符串:
import json
from flask import Response
@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 Response(json.dumps(documents_), mimetype='application/json')
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.