简体   繁体   English

NDB AppEngine NoneType错误

[英]ndb appengine nonetype error

I am having an issue with google app engine. 我在使用Google App Engine时遇到问题。 I am trying to implement a Person ndb model that will store basic info on a person. 我正在尝试实现一个Person ndb模型,该模型将存储一个人的基本信息。 When I try to query the database for a key though, I keep getting a Nonetype error in traceback: 但是,当我尝试在数据库中查询密钥时,在回溯中始终出现Nonetype错误:

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). 注意:所有缩进在文件中都是正确的,只是笨拙地复制粘贴了(特别是MainPageLI段,文件的前几行在选项卡中被选中)。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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