我刚开始使用 MongoDb,但在使用 find_one() 时卡住了。find() 函数完美运行,但使用 find_one() 时返回 None 类型。我被困在这一点上。下面是我正在使用的代码。
#mongo.py
@app.route('/update_user/<emp_id>', methods=['GET', 'POST'])
def update_user(emp_id):
print("Employee ID:", emp_id)
output = list(emp.find_one({'emp_id': emp_id}))
print("Output:", output)
if request.method == 'POST':
if not request.form.get['emp_id'] or not request.form.get['first_name'] or not request.form.get['last_name'] \
or not request.form.get['dept_name'] or not request.form.get['gender'] or not request.form.get['dob'] \
or not request.form.get['salary'] or not request.form['country_code'] \
or not request.form.get['mobile_no']:
flash('Please enter all the fields', 'error')
else:
emp_id = int(request.form.get('emp_id'))
first_name = request.form.get('first_name')
last_name = request.form.get('last_name')
dept_name = request.form.get('dept_name')
gender = request.form.get('gender')
dob = request.form.get('dob')
salary = float(request.form.get('salary'))
country_code = int(request.form['country_code'])
mobile_no = int(request.form.get('mobile_no'))
emp.update_one({"emp_id": emp_id, "first_name": first_name, "last_name": last_name,
"dept_name": dept_name, "gender": gender, "dob": dob, "salary": salary,
"country_code": country_code, "mobile_no": mobile_no})
flash('Employee Updated')
return redirect('user.html')
return render_template('update_user.html', emp_id=emp_id, output=output)
它抛出以下错误:
File "D:\projects\EmpMgmtFlask\mongo.py", line 37, in update_user
output = list(emp.find_one({'emp_id': emp_id}))
TypeError: 'NoneType' object is not iterable