提问人:Cherry Toska 提问时间:7/12/2022 最后编辑:Cherry Toska 更新时间:7/14/2022 访问量:391
尝试使用 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
问:
我正在尝试将 firebase 与 python 一起使用。 我已连接到数据库并在方案中如下:
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()
目前数据库有 2 种类型的文档,当我们将它们转换为字典时,我们会得到以下内容:
Admin: {'users': [], 'email': '...', 'first_name': '...', 'last_name': '...'}
User: {'email': '...', 'onboarding_form': 'first_name': '...', 'last_name': '...'}
我想将用户的引用添加到用户数组中,该数组是 admin 的字段。对于它,我有以下代码:
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])})
但代码不会插入对用户数组的引用。我收到以下错误:
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"
}
}
.
我不得不省略名称和reference_value的开头,因此它们是“...”。我不明白这个错误,是什么原因导致了这个错误?提供字段路径不应该是正确的吗?
当我尝试获取管理员的信息时,我得到以下信息:
print(admin_ref.get().id, " => ", admin_ref.get().to_dict())
{'users': [], 'email': '...', 'first_name': '...', 'last_name': '...'}
使用的 firebase_admin 和 protovuf 版本是:
Name: firebase-admin
Version: 5.2.0
Name: protobuf
Version: 4.21.2
user_ref(编辑前的前physio_ref,这个名字不应该出现在问题中)。我想添加到名为“users”的数组中的数据是对另一个用户的引用。
答:
4赞
Marc Anthony B
7/14/2022
#1
使用最新版本的 Google Cloud Firestore 存在已知问题。正如 @Cherry Toska 所证实的那样,降级以解决问题。protobuf
protobuf
3.20.1
pip3 install protobuf==3.20.1
评论
physio_ref
firebase-admin
protobuf
protobuf
3.20.1
sudo pip3 install protobuf==3.20.1