简体   繁体   中英

Why do I get error "Expected a message object, but got kind: " when using Datastore API?

I'm using this Datastore API documentation and here is my code in Python:

class DatastoreConf:
    def __init__(self):
        self.datastore_client = datastore.Client()

    def insert_entity(self):

        complete_key = self.datastore_client.key("Task", "sampleTask")
        task = datastore.Entity(key=complete_key)
        task.update(
            {
                "category": "Personal",
                "done": False,
                "priority": 4,
                "description": "Learn Cloud Datastore",
            }
        )
        self.datastore_client.put(task)

d = DatastoreConf()
d.insert_entity()

I've already set GOOGLE_APPLICATION_CREDENTIALS variable to my service account key and I have sufficient roles, but I'm getting the error TypeError: Expected a message object, but got kind: "Task" name: "sampleTask" .

The traceback that I got is:

Traceback (most recent call last):
  File "C:/Users/user/project/datastore_configuration.py", line 24, in <module>
    d.get_entity()
  File "C:/Users/user/project/datastore_configuration.py", line 21, in get_entity 
    self.datastore_client.put(task) 
  File "C:\Users\user\project\venv\lib\site-packages\google\cloud\datastore\client.py", line 575, in put
    self.put_multi(entities=[entity], retry=retry, timeout=timeout) 
  File "C:\Users\user\projectvenv\lib\site-packages\google\cloud\datastore\client.py", line 612, in put_multi 
    current.put(entity) 
  File "C:\Users\user\project\venv\lib\site-packages\google\cloud\datastore\batch.py", line 227, in put 
    _assign_entity_to_pb(entity_pb, entity) 
  File "C:\Users\user\project\venv\lib\site-packages\google\cloud\datastore\batch.py", line 373, in _assign_entity_to_pb 
    bare_entity_pb = helpers.entity_to_protobuf(entity) 
  File "C:\Users\user\project\venv\lib\site-packages\google\cloud\datastore\helpers.py", line 207, in entity_to_protobuf 
    key_pb = entity.key.to_protobuf() 
  File "C:\Users\user\project\venv\lib\site-packages\google\cloud\datastore\key.py", line 298, in to_protobuf 
    key.path.append(element)

Do you have any idea what causes this error?

I resolved the problem. The error was due to version difference. Datastore worked with library protobuf 3.20.1 but didn't with 4.21.0. I think it's beacause Firestore is now developed much more and the versions in Datastore are no longer supported.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM