2015-01-04 80 views
0

我在爲Google App Engine上的Python查詢獲取正確響應方面遇到問題。Google App Engine數據存儲上的Python API查詢

這是來自GAE的LOG,它成功地打印出與查詢參數匹配的對象,但現在我試圖將該對象數據作爲JSON發回。我正在創建一個基本的用戶認證系統,該系統使用電子郵件和密碼查詢USER對象,如果存在,則返回所有數據。

如何分解此查詢以返回找到的數據?

E 00:21:06.352 Encountered unexpected error from ProtoRPC method implementation: AttributeError ('list' object has no attribute 'email') 
    Traceback (most recent call last): 
    File "/base/data/home/runtimes/python27/python27_lib/versions/third_party/protorpc-1.0/protorpc/wsgi/service.py", line 181, in protorpc_service_app 
     response = method(instance, request) 
    File "/base/data/home/runtimes/python27/python27_lib/versions/third_party/endpoints-1.0/endpoints/api_config.py", line 1332, in invoke_remote 
     return remote_method(service_instance, request) 
    File "/base/data/home/runtimes/python27/python27_lib/versions/third_party/protorpc-1.0/protorpc/remote.py", line 412, in invoke_remote_method 
     response = method(service_instance, request) 
    File "/base/data/home/apps/s~caramel-theory-800/1.381271940209425490/photoswap_api.py", line 34, in user_auth 
     return UserCreateResponseMessage(email=query.email, password=query.password, username=query.username, 
    AttributeError: 'list' object has no attribute 'email' 
E 00:21:06.360 [User(key=Key('User', 5086441721823232), email=u'pop', password=u'top', username=u'yop')] 

這裏是端點API

我相信這個問題是代碼的最後一行的地方改掉返回查詢(UserAuthResponseMessage)的結果中..

@endpoints.method(UserAuthRequestMessage, UserAuthResponseMessage, 
        path='user', http_method='GET', 
        name='user.auth') 
def user_auth(self, request): 
    # create some type of query to check for email address, and than check to see if passwords match 
    query = User.query(User.email == request.email, User.password == request.password).fetch() 
    print query 

    # return the info from the server 
    return UserCreateResponseMessage(email=query[0].email, password=query[0].password, username=query[0].username, 
            id=query[0].key.id()) 


APPLICATION = endpoints.api_server([PhotoswapAPI], restricted=False) 

回答

1

你的問題是你正在執行一個查詢,並期望引用該查詢的電子郵件屬性,當然它沒有。

如果您期望查詢只有一個結果,那麼您應該使用get而不是索引尋址。

另一個問題是user_auth包含的方法與您的堆棧跟蹤不匹配。所以這裏有一個問題。

0

在蒂姆霍夫曼的幫助下,這是我能夠想出的解決方案。

我回來了錯誤的ResponseMessage,我最好使用.get而不是.fetch,因爲應該只返回1個結果。

@endpoints.api(name='photoswap', version='v1') 
class PhotoswapAPI(remote.Service): 
    @endpoints.method(UserCreateRequestMessage, UserCreateResponseMessage, 
         path='user', http_method='POST', 
         name='user.create') 
    def user_create(self, request): 
     entity = User(email=request.email, password=request.password, username=request.username) 
     entity.put() 
     return UserCreateResponseMessage(email=entity.email, password=entity.password, username=entity.username, 
             id=entity.key.id()) 

    @endpoints.method(UserAuthRequestMessage, UserAuthResponseMessage, 
         path='user', http_method='GET', 
         name='user.auth') 
    def user_auth(self, request): 
     # create some type of query to check for email address, and than check to see if passwords match 
     query = User.query(User.email == request.email, User.password == request.password).get() 
     print query 

     # return the info from the server 
     return UserAuthResponseMessage(email=query.email, password=query.password, username=query.username, id=query.key.id()) 


APPLICATION = endpoints.api_server([PhotoswapAPI], restricted=False) 
+0

此「答案」中的代碼仍然不正確,請根據其前面的文本進行編輯和糾正。 –

+0

我不太清楚我失蹤的是什麼。我利用.get()返回1結果,並且我能夠引用來自查詢的電子郵件,密碼,用戶名和ID值。返回的JSON是我在響應消息中查找的內容。你能否詳細說明那是什麼「錯誤」? –

相關問題