简体   繁体   中英

Fire gives the "TypeError: Expected a message object, but got field_path" error when trying to use ArrayUnion

I am trying to use firebase with python. I have connected to the database and in the scheme as following:

import firebase_admin
from firebase_admin import credentials, db, firestore

# Initialize connection to firebase
cred = credentials.Certificate("serviceAccountKey.json")
app_options = {
    'databaseURL': '...'}
firebase_admin.initialize_app(cred)
db = firestore.client()

Currently the database has 2 types of documents, we get the following when we convert them to dictionaries:

Admin: {'users': [], 'email': '...', 'first_name': '...', 'last_name': '...'}
User: {'email': '...', 'onboarding_form': 'first_name': '...', 'last_name': '...'}

I want to add a reference of a user to the array of users which is a field of admin. For it I have the following code:

    user_id = get_user_id(user_last_name, user_first_name)
    admin_id = get_admin_id(admin_last_name, admin_first_name)

    user_ref = db.collection(u'user').document(user_id)
    admin_ref = db.collection(u'admin').document(admin_id)

    admin_ref.update(
        {u'users': firestore.ArrayUnion([user_ref])})

But the code does not insert the reference to array of users. I get the following error:

Traceback (most recent call last):
  File "/home/primrose/work/BE/db_api.py", line 109, in <module>
    test_run()
  File "/home/primrose/work/BE/db_api.py", line 106, in test_run
    add_user("...", "...", "...", "...")
  File "/home/primrose/work/BE/db_api.py", line 98, in add_user
    admin_ref.update(
  File "/home/primrose/.local/lib/python3.10/site-packages/google/cloud/firestore_v1/document.py", line 324, in update
    batch, kwargs = self._prep_update(field_updates, option, retry, timeout)
  File "/home/primrose/.local/lib/python3.10/site-packages/google/cloud/firestore_v1/base_document.py", line 239, in _prep_update
    batch.update(self, field_updates, option=option)
  File "/home/primrose/.local/lib/python3.10/site-packages/google/cloud/firestore_v1/base_batch.py", line 141, in update
    write_pbs = _helpers.pbs_for_update(
  File "/home/primrose/.local/lib/python3.10/site-packages/google/cloud/firestore_v1/_helpers.py", line 941, in pbs_for_update
    update_pb.update_transforms.extend(field_transform_pbs)
TypeError: Expected a message object, but got field_path: "users"
append_missing_elements {
  values {
    reference_value: ".../databases/(default)/documents/user/hlCcG1oFW4Z75JKqp1ZcfM8xWlh1"
  }
}
.

I had to omit the names and the beginning of the reference_value therefore they are "...". I do not understand the error, what could have caused the error? Shouldn't providing a field path be right?

When I try to get the information of the admin I get the following:

    print(admin_ref.get().id, " => ", admin_ref.get().to_dict())
    {'users': [], 'email': '...', 'first_name': '...', 'last_name': '...'}

The used firebase_admin and protovuf version are:

Name: firebase-admin
Version: 5.2.0

Name: protobuf
Version: 4.21.2

The user_ref (former physio_ref before the edit, that name was not supposed to be in the question). the data I want to add to the array called "users" is a reference to another user.

There are known issues with Google Cloud Firestore using the latest version of protobuf . As confirmed by @Cherry Toska, downgrading protobuf to 3.20.1 solves the issue.

pip3 install protobuf==3.20.1

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