Skip to content
Advertisement

Validate data in Dynamodb is only working through if data is present

this is a rather easy and silly question but I can’t seem to understand the problem at hand.

I am trying to create a register page where user could enter thier email, if their email is present, then the the function will put items into the database, if not, it will return “email is already present.

def register():
    if request.method=='POST':
        #get user information from form
        email=request.form['email']
        username=request.form['username']
        password=request.form['password']
        #get table
        table=dynamodb.Table('login')
        #query table and comapre email
        response = table.query(
                KeyConditionExpression=Key('email').eq(email)
        )
        #put results in items
        items = response['Items']
        #validate email
        if email != items[10]['email']:
            #put user information into database table
            table.put_item(
                Item={
                    'email':email,
                    'user_name':username,
                    'password':password
                }
            )
            #redirect user to login page after signup
            flash('You are signed up! Please login!')
            return render_template('login.html')

        flash("Email already Exists")
    return render_template('register.html') 

EDIT:-

def register():
    if request.method=='POST':
        #get user information from form
        email=request.form['email']
        username=request.form['username']
        password=request.form['password']
        #get table
        table=dynamodb.Table('login')
        #query table and comapre email
        response = table.query(
                KeyConditionExpression=Key('email').eq(email)
        )
        #put results in items
        items = response['Items']
        if not items:
            #validate email
            if email != items[0]['email']:
                #put user information into database table
                table.put_item(
                    Item={
                        'email':email,
                        'user_name':username,
                        'password':password
                    }
                )
                #redirect user to login page after signup
                flash('You are signed up! Please login!')
                return render_template('login.html')
        else:
            print("list empty!")

        flash("Email already Exists")
    return render_template('register.html')

My problem is , when a user enters an email that is already present, the function will return “email already exists”, but when a user enters a unique email, the if statement throws an “list index out range”. I know exactly what this error means, but what i don’t under is that what does the function renders this error?! I have 10 entries in my dynamodb and when i enter 10 instead of 0, I still get the same error.

Help is greatly appreciated.

Advertisement

Answer

You should first check if items list is empty or not. As you are entering unique email, items will be an empty list.

If list does not have any item and you check for 0th index it will throw list index out of range exception.

User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement