ndb appengine nonetype error

380 views Asked by At

I am having an issue with google app engine. I am trying to implement a Person ndb model that will store basic info on a person. When I try to query the database for a key though, I keep getting a Nonetype error in traceback:

File "xxxx", line 104, in get
    parent = ndb.Key('Person', users.get_current_user().email())
AttributeError: 'NoneType' object has no attribute 'email'

Here is the code that relates to the error:

This is where I declare the model

class Person(ndb.Model):
    dev_id = ndb.StringProperty()
    num_readings = ndb.IntegerProperty()

And here I am simply trying to use it later in the same file:

class MainPageLI(webapp2.RequestHandler):
# Front page for those logged in
def get(self):
    user = users.get_current_user()
    parent = ndb.Key('Person', users.get_current_user().email())
    person = parent.get()

    if person == None:
        person = Person(id=users.get_current_user().email())

        person.temperature_unit = 'celsius'

        person.time_zone = 8.00

        """person.notify_type = ['by-time']
        person.notify_time_value = 4
        person.notify_time_unit = 'days'
        person.notify_parameters = ['by-temp']
        person.notify_temperature_abe = 'exactly'
        person.notify_temperature_value = 20
        person.notify_temperature_unit = 'celsius'
        person.notify_light_abe = 'above'
        person.notify_light_value = 90
        person.notify_motion = 'present'"""

        person.num_readings = 5

        #person.history_log_value = 2
        #person.history_log_unit = 'days'

        person.put()

    if user:  # signed in already
        params = urllib.urlencode({'username':users.get_current_user().nickname()})
        template_values = {
            'user_mail': users.get_current_user().email(),
            'logout': users.create_logout_url(self.request.host_url),
            'nickname': users.get_current_user().nickname(),
        }
        template = jinja_environment.get_template('frontuser.html')
        self.response.out.write(template.render(template_values))
    else:
        self.redirect(self.request.host_url)

NOTE: All the indenting is proper in the file, it just copy-pasted awkwardly (especially the MainPageLI segment, the first few lines are tabbed right in the file).

Any and all help would be greatly appreciated! thanks :)

2

There are 2 answers

0
Dan Cornilescu On BEST ANSWER

You need to move your if user: # signed in already check higher up, before the users.get_current_user().email() call (which you can replace with user.email() BTW, since you already obtained user above).

0
Aerodyno On

Here's your problem:

users.get_current_user()  # returns None, and you can't call .email on None.
# check if it returns something usable first. :)