1

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

我的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)

这是我定义的用于检索实体的 API 方法'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'时,我得到了

404 未找到

{“error_message”:“找不到配方。”,“state”:“APPLICATION_ERROR”}

谁能指出我是否在某处遗漏了什么?

注意:见评论。用于阅读的问题中的错误

400 错误请求

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

但是@sentiki能够解决这个先前的错误。

4

1 回答 1

1

404是预期的。该id属性的“魔力”在于它调用了 UpdateFromKey

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

通过使用request_fields=('title',),您将拥有一个简单的数据属性,而不是 an EndpointsAliasProperty,因此只设置了值。结果,_from_datastore永远不会设置和您的检查

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

endpoints.NotFoundException正如预期的那样抛出一个。

于 2013-05-22T17:29:17.470 回答