简体   繁体   English

尝试使用 ArrayUnion 时,Fire 给出“TypeError: Expected a message object, but got field_path”错误

[英]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.我正在尝试将 firebase 与 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 "...".我不得不省略reference_value 的名称和开头,因此它们是“...”。 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:使用的 firebase_admin 和 protovuf 版本是:

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). user_ref(编辑前的前 physio_ref,该名称不应该出现在问题中)。 the data I want to add to the array called "users" is a reference to another user.我要添加到名为“users”的数组中的数据是对另一个用户的引用。

There are known issues with Google Cloud Firestore using the latest version of protobuf .使用最新版本的protobuf的 Google Cloud Firestore 存在已知问题。 As confirmed by @Cherry Toska, downgrading protobuf to 3.20.1 solves the issue.正如@Cherry Toska 所证实的,将protobuf降级到3.20.1可以解决问题。

pip3 install protobuf==3.20.1

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 'TypeError: Expected a message Descriptor, got Descriptor' when trying to import keras - 'TypeError: Expected a message Descriptor, got Descriptor' when trying to import keras Python TypeError - 预期的字节数,但在尝试创建签名时得到&#39;str&#39; - Python TypeError - Expected bytes but got 'str' when trying to created signature gspread给出TypeError:尝试登录时预期的字节数 - gspread gives TypeError: expected bytes when trying to log in 为什么我在使用 Datastore API 时收到错误消息“期望消息对象,但得到了一种:”? - Why do I get error "Expected a message object, but got kind: " when using Datastore API? 出现错误:尝试继承员工表单时,字段“message_follower_ids”不存在 - Got Error: Field `message_follower_ids` does not exist when trying inherit employee form 视图中的类型错误:字段需要一个数字,但得到了 &#39; &#39; - TypeError in view: Field expected a number but got ' ' 类型错误:字段 &#39;id&#39; 需要一个数字,但得到了 (() - TypeError: Field 'id' expected a number but got (() 类型错误:字段“id”需要一个数字但得到了<User: 1> - TypeError: Field 'id' expected a number but got <User: 1> TypeError:字段“id”需要一个数字,但得到了 OrderedDict - TypeError: Field 'id' expected a number but got OrderedDict 我正在尝试在 for 循环中使用 int 但错误消息预期类型 &#39;collections.iterable&#39; got &#39;int&#39; 反而出现 - I'm trying to use int in a for loop but the error message expected type 'collections.iterable' got 'int' instead appears
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM