GAE 端点错误:预期的 FooBar 实例,得到 FooBar() [英] GAE Endpoints error: Expected FooBar Instance, got FooBar()

查看:35
本文介绍了GAE 端点错误:预期的 FooBar 实例,得到 FooBar()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Google Cloud Endpoints 管理 API,并且正在努力解决一个奇怪的、随机发生的错误.错误仅发生在生产中,单元测试通过属性.

I'm managing an API with using Google Cloud Endpoints and am struggling with a strange, randomly happening, error. The error happens in production only, the unit tests pass property.

我有以下模型:

class UserFacebookData(ndb.Model):
  facebook_id = ndb.StringProperty(required=True, indexed=True)
  facebook_token = ndb.StringProperty(required=True, indexed=True)

class User(ndb.Model, Entity): 
  created = ndb.DateTimeProperty(auto_now_add=True, indexed=True)
  username = ndb.StringProperty(indexed=True)
  facebook_data = ndb.StructuredProperty(UserFacebookData, indexed=False)

并使用它来创建用户

ufd = UserFacebookData(facebook_id=some_api_data, facebook_token=some_api_data)
user = User(username=some_api_data, facebook_data=ufd)
user.save()

或更新一个

ufd = UserFacebookData(facebook_id=some_api_data, facebook_token=some_api_data)    
user = get_from_db(some_api_data)
user.facebook_data = ufd
user.save()

(save() 是父实体类的一个方法,做一个 put() + 一些 memcached 的东西)

( save() is a method of the parent Entity class, doing a put() + some memcached stuff)

并且在更新用户时不时出现以下错误

And got, from time to time when updating the user, the following error

Expected UserFacebookData instance, 
got UserFacebookData(facebook_id=u'xxx', facebook_token=xxx'))

然而,跟踪(打印和检查 ufd/UserFacebookData 实例的类型)显示它已正确创建并且用户也存在

Yet, traces (to print and check type of the ufd / UserFacebookData instance) showed that it's correctly created and that the user also exists

# print user.username
my_username
# print UserFacebookData
UserFacebookData(facebook_id=u'xx', facebook_token=xx')
# print type(UserFacebookData)
UserFacebookData<facebook_id=StringProperty('facebook_id', required=True), 
facebook_token=StringProperty('facebook_token', required=True)>

我看不到我错过了什么.有没有人已经经历过 - 并希望修复它?这是完整的错误堆栈

I can't see what I'm missing. Has someone already experiences that - and hopefully fixed it? Here's the full error stack

Encountered unexpected error from ProtoRPC method implementation: BadValueError (Expected UserFacebookData instance, got UserFacebookData(facebook_id=u'xx', facebook_token=xx'))
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 1329, 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~yapme-web/alpha2.377359917212322950/api/helpers.py", line 43, in method
return func(*args, **kwargs)
  File "/base/data/home/apps/s~xx-web/alpha2.377359917212322950/api/user.py", line 25, in users_create
user = User.create(request)
  File "/base/data/home/apps/s~xx-web/alpha2.377359917212322950/entities/users.py", line 168, in create
user.facebook_data = facebook_data
  File "/base/data/home/runtimes/python27/python27_lib/versions/1/google/appengine/ext/ndb/model.py", line 1339, in __set__
    self._set_value(entity, value)
  File "/base/data/home/runtimes/python27/python27_lib/versions/1/google/appengine/ext/ndb/model.py", line 1084, in _set_value
value = self._do_validate(value)
  File "/base/data/home/runtimes/python27/python27_lib/versions/1/google/appengine/ext/ndb/model.py", line 1031, in _do_validate
value = self._call_shallow_validation(value)
  File "/base/data/home/runtimes/python27/python27_lib/versions/1/google/appengine/ext/ndb/model.py", line 1223, in _call_shallow_validation
return call(value)
  File "/base/data/home/runtimes/python27/python27_lib/versions/1/google/appengine/ext/ndb/model.py", line 1270, in call
newvalue = method(self, value)
  File "/base/data/home/runtimes/python27/python27_lib/versions/1/google/appengine/ext/ndb/model.py", line 2269, in _validate
(self._modelclass.__name__, value))
BadValueError: Expected UserFacebookData instance, got UserFacebookData(facebook_id=u'xx', facebook_token=xx')

推荐答案

我遇到了同样的问题,并已在此处发布答案:App Engine保存 ndb LocalStructured 实体时出现 BadValueError.基本上问题是由相对导入引起的,所以python认为FooBar在不同的包中.

I had the same problem, and have posted the answer here: App Engine BadValueError when saving ndb LocalStructured entity. Basically the problem was caused by a relative import, so that python thinks FooBar are in different packages.

这篇关于GAE 端点错误:预期的 FooBar 实例,得到 FooBar()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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