简体   繁体   中英

ndb appengine nonetype error

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 :)

您需要移动if user: # signed in already检查更高级别,然后再users.get_current_user().email() (您可以用user.email() BTW替换,因为您已经在上面获得了user )。

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. :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM