尝试检索单个实体时出错 [英] Error when trying to retrieve a single entity

查看:248
本文介绍了尝试检索单个实体时出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在过去的几天里,我一直在使用Google Cloud Endpoints(目的是将它与AngularJS挂钩),当我尝试从数据存储中检索单个实体时遇到了麻烦.

I've been playing around with Google Cloud Endpoints for the past few days (aiming to hook it up with AngularJS) and I ran into a bit of trouble when I try to retrieve a single entity from my datastore.

我的ndb模型设置为:

class Ingredients(EndpointsModel):
    ingredient = ndb.StringProperty()

class Recipe(EndpointsModel):
    title = ndb.StringProperty(required=True)
    description = ndb.StringProperty(required=True)
    ingredients = ndb.StructuredProperty(Ingredients, repeated=True)
    instructions = ndb.StringProperty(required=True)

这是我定义的用于通过'title'检索实体的API方法:

Here is the API method I've defined to retrieve the entity by 'title':

    @Recipe.method(request_fields=('title',), path='recipe/{title}',
                   http_method='GET', name='recipe.get')
    def get_recipe(self, recipe):
        if not recipe.from_datastore:
            raise endpoints.NotFoundException('Recipe not found.')
        return recipe   

如果我要使用'id'(由EndpointsModel提供的帮助器方法)代替'title'的请求字段,则API方法可以正常工作.但是,当我使用'title'时,我会得到

The API method works fine if I were to use 'id' (helper methods provided by EndpointsModel) in place of 'title' for the request fields. When I use 'title', however, I'm getting

找不到404

404 Not Found

{"error_message":找不到配方.","state":"APPLICATION_ERROR"}

{"error_message": "Recipe not found.","state": "APPLICATION_ERROR"}

有人可以指出我是否在某处丢失了东西吗?

Can anyone point out if I am missing something somewhere?

注意:查看评论.用于阅读的问题中的错误

NOTE: See comments. The error in the question used to read

400错误的请求

400 Bad Request

{"error_message":解析ProtoRPC请求时出错(无法解析请求内容:消息RecipeProto_title缺少必填字段标题)", "state":"REQUEST_ERROR"}

{"error_message": "Error parsing ProtoRPC request (Unable to parse request content: Message RecipeProto_title is missing required field title)", "state": "REQUEST_ERROR"}

但是 @sentiki 能够解决此先前的错误.

but @sentiki was able to resolve this previous error.

推荐答案

404是预期的. id属性的魔力"是它调用 UpdateFromKey .

The 404 is expected. The "magic" of the id property is that it calls UpdateFromKey.

此方法尝试根据请求在实体上设置ndb.Key,然后尝试检索使用该键存储的实体.如果实体存在,则将数据存储区中的值复制到从请求中解析的实体,然后_from_datastore属性为

This method tries to set an ndb.Key on the entity based on the request and then attempts to retrieve the entity stored with that key. If the entity exists, the values from the datastore are copied over to the entity parsed from the request and then the _from_datastore property is set to True.

通过使用request_fields=('title',),您将拥有简单的数据属性,而不是

By using request_fields=('title',), you have a simple data property rather than an EndpointsAliasProperty and so only the values are set. As a result, _from_datastore never gets set and your check

    if not recipe.from_datastore:
        raise endpoints.NotFoundException('Recipe not found.')

按预期抛出endpoints.NotFoundException.

这篇关于尝试检索单个实体时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆