I just started using MongoDb and I am stuck when using find_one(). The find() function works perfectly but None type is returned when using find_one(). I am stuck at this point. Below is the code that I am using.
#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)
It throws the below error:
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
find_one()
doesn't return a cursor; it returns a single dictionary; so you don't need to wrap it in a list
function.
output = emp.find_one({'emp_id': emp_id})