Python ProtocolBufferDecodeError:使用ndb.Key时被截断

Python ProtocolBufferDecodeError:使用ndb.Key时被截断,python,google-app-engine,app-engine-ndb,Python,Google App Engine,App Engine Ndb,我有以下代码,在升级GAE Python NDB之前,这些代码都可以使用: class MyHandler(webapp2.RequestHandler): def get(self,urlString): resume = ndb.Key(urlsafe=urlString).get() 现在,我有一个错误: Traceback (most recent call last): File "C:\Program Files (x86)\Google\google_appeng

我有以下代码,在升级GAE Python NDB之前,这些代码都可以使用:

class MyHandler(webapp2.RequestHandler):
  def get(self,urlString):
    resume = ndb.Key(urlsafe=urlString).get()
现在,我有一个错误:

Traceback (most recent call last):
  File "C:\Program Files (x86)\Google\google_appengine\lib\webapp2-2.5.2\webapp2.py", line 1535, in __call__
    rv = self.handle_exception(request, response, e)
  File "C:\Program Files (x86)\Google\google_appengine\lib\webapp2-2.5.2\webapp2.py", line 1529, in __call__
    rv = self.router.dispatch(request, response)
  File "C:\Program Files (x86)\Google\google_appengine\lib\webapp2-2.5.2\webapp2.py", line 1278, in default_dispatcher
    return route.handler_adapter(request, response)
  File "C:\Program Files (x86)\Google\google_appengine\lib\webapp2-2.5.2\webapp2.py", line 1102, in __call__
    return handler.dispatch()
  File "C:\Program Files (x86)\Google\google_appengine\lib\webapp2-2.5.2\webapp2.py", line 572, in dispatch
    return self.handle_exception(e, self.app.debug)
  File "C:\Program Files (x86)\Google\google_appengine\lib\webapp2-2.5.2\webapp2.py", line 570, in dispatch
    return method(*args, **kwargs)
  File "C:\xampp\htdocs\mapjobs\main.py", line 127, in get
    resume_key = ndb.Key(urlsafe=urlString)
  File "C:\Program Files (x86)\Google\google_appengine\google\appengine\ext\ndb\key.py", line 212, in __new__
    self.__reference = _ConstructReference(cls, **kwargs)
  File "C:\Program Files (x86)\Google\google_appengine\google\appengine\ext\ndb\utils.py", line 136, in positional_wrapper
    return wrapped(*args, **kwds)
  File "C:\Program Files (x86)\Google\google_appengine\google\appengine\ext\ndb\key.py", line 642, in _ConstructReference
    reference = _ReferenceFromSerialized(serialized)
  File "C:\Program Files (x86)\Google\google_appengine\google\appengine\ext\ndb\key.py", line 774, in _ReferenceFromSerialized
    return entity_pb.Reference(serialized)
  File "C:\Program Files (x86)\Google\google_appengine\google\appengine\datastore\entity_pb.py", line 1791, in __init__
    if contents is not None: self.MergeFromString(contents)
  File "C:\Program Files (x86)\Google\google_appengine\google\net\proto\ProtocolBuffer.py", line 84, in MergeFromString
    self.MergePartialFromString(s)
  File "C:\Program Files (x86)\Google\google_appengine\google\net\proto\ProtocolBuffer.py", line 98, in MergePartialFromString
    self.TryMerge(d)
  File "C:\Program Files (x86)\Google\google_appengine\google\appengine\datastore\entity_pb.py", line 1920, in TryMerge
    d.skipData(tt)
  File "C:\Program Files (x86)\Google\google_appengine\google\net\proto\ProtocolBuffer.py", line 524, in skipData
    self.skip(4)
  File "C:\Program Files (x86)\Google\google_appengine\google\net\proto\ProtocolBuffer.py", line 499, in skip
    if self.idx + n > self.limit: raise ProtocolBufferDecodeError, "truncated"
ProtocolBufferDecodeError: truncated
重点可能是:

File "C:\Program Files (x86)\Google\google_appengine\lib\webapp2-2.5.2\webapp2.py", line 570, in dispatch
  return method(*args, **kwargs)
File "C:\xampp\htdocs\mapjobs\main.py", line 127, in get
  resume_key = ndb.Key(urlsafe=urlString)

怎么了?

我猜您是通过url参数接收作为url安全字符串的密钥,由于浏览器中的最大url长度限制,该参数会被截断。实际上,浏览器将Url长度限制为最多2000个字符。见这个问题:

如果您的密钥包含(多个)父密钥,则可以超过2000个字符。是这样吗?请在创建和收到urlsafe编码密钥时检查其长度


如果是这种情况,那么解决方法就是只使用实体的ID(或父密钥的实体)并手动构造密钥。

我与url不匹配,其中:

app = webapp2.WSGIApplication([
    ('/', MainHandler),
    ('/jobs',JobsHandler),
    ('/job/(.*)',JobHandler),
    ('/job/(.*)/update', JobUpdateHandler),
    ('/job/(.*)/delete', JobDeleteHandler),
    ('/job/create', JobCreateHandler),
    ('/resume/(.*)',ResumeHandler),
    ('/resume/(.*)/update', ResumeUpdateHandler),
    ('/resume/(.*)/delete', ResumeDeleteHandler),
    ('/resume/create', ResumeCreateHandler),
    ('/resumes',ResumesHandler),
    ('/profile',ProfileHandler),
    ('/profile/(.*)/update', ProfileUpdateHandler),
], debug=True)
请注意,我错误地安排了“/resume/(.*)”,因为它应该位于最后一个接收urlString的底部。这就是现在的情况:

app = webapp2.WSGIApplication([
    ('/', MainHandler),
    ('/jobs',JobsHandler),
    ('/job/(.*)/update', JobUpdateHandler),
    ('/job/(.*)/delete', JobDeleteHandler),
    ('/job/create', JobCreateHandler),
    ('/job/(.*)',JobHandler),
    ('/resume/(.*)/update', ResumeUpdateHandler),
    ('/resume/(.*)/delete', ResumeDeleteHandler),
    ('/resume/create', ResumeCreateHandler),
    ('/resume/(.*)',ResumeHandler),
    ('/resumes',ResumesHandler),
    ('/profile',ProfileHandler),
    ('/profile/(.*)/update', ProfileUpdateHandler),
], debug=True)

我以前不知怎么解决了这个问题,但由于思考过度,有些事情我看错了。所以,我认为这是一件好事,我把它作为一个笔记给自己。我希望错误消息可以更明显。我可以看到url顺序如何阻止您访问创建/更新/删除处理程序。。。我不知道这怎么能修复你的协议错误,老实说,我也不知道,但它只是修复了它。我想在错误处理方面应该做些什么。也许…我想我现在明白了。我把webapp2接收器安排错了。我指的是“/resume/(*)”。我会发布我的答案,请稍等。不过,这也有可能。谢谢