0

I am learning Flask framework and creating a blog website using MongoDB as my back end db. I am perfectly able to save my data into a collection as a document, but I have trouble retrieving a saved document from my collection.

I am using an Azure Cosmos DB MongoDB API for my collection. Please find the necessary screenshots below.

Document Class Definition

class UserDocument(Document, UserMixin):

username = StringField(min_length=2,max_length=100,required=True,unique=True)
email = EmailField(required=True,unique=True)
password = StringField(max_length=60,required=True,unique=True)
remember = BooleanField(required=True,default=True,unique=True)

meta = { 'collection' : 'UserDocument'}

Register route - Saving my document

@app.route("/register",methods= ["POST","GET"] )
def register():
    form = RegistrationForm()
    if form.validate_on_submit():
        if request.method == "POST":
            user = UserDocument()
            user.username = form.username.data
            user.email = form.email.data
            user.password = bcrypt.generate_password_hash(form.password.data).decode('utf-8')
            user.save()        
        flash(form.password.data,"Info")
    return render_template('register.html',form=form,title='Register')

MongoEngine documentation - for retrieving enter image description here

Issue in retrieving document

@app.route("/login",methods= ["POST","GET"] )
def login():
    form = LoginForm()
    if form.validate_on_submit():
        if request.method == "POST":
            # table = client.db.MongoEngineDB
            # user = table.UserDocument.find_one({'email':form.email.data})
            user = UserDocument.objects(username=form.email.data)
            flash(user, "Info")
    return render_template('login.html',form=form,title='Login')

I am not sure where I am messing up, I did spend a lot of time trying to figure out myself by going through lot of contents but unable to find out the root cause. Not sure if this has to do with any missing modules or the "UserDocument" class definition.

Not much information available in the "flask-mongoengine" documentation as well.

Prakazz
  • 421
  • 1
  • 8
  • 21
  • Please add the code, as code, not Screenshots – DarkSuniuM Oct 14 '18 at 18:33
  • 1
    @DarkSuniuM Done. – Prakazz Oct 15 '18 at 03:02
  • "Issue" is a bit broad. Do you get any exceptions or errors? How do you process the result of the query? Do you loop over it? Because `UserDocument.objects()` will return a query set so you should either iterate over or if you really want to get just one object add `.first()` to the call to return the first matching object or None instead of a query set. – Roman Kutlak Oct 31 '18 at 11:48

0 Answers0