Skip to main content
 首页 » 编程设计

python之通过 ArrayUnion 更新 firestore 数据库

2024年11月24日7java哥

我想通过创建 ArrayUnion 对象来更新我的 firestore 数据库。 不幸的是,我的更新方法产生了以下错误:

 TypeError: ('Cannot convert to a Firestore Value', <google.cloud.firestore_v1beta1.transforms.ArrayUnion object at 0x04CDEF90>, 
'Invalid type', <class 'google.cloud.firestore_v1beta1.transforms.ArrayUnion'>) 

我的方法是基于官方文档https://github.com/googleapis/google-cloud-python/blob/master/firestore/google/cloud/firestore_v1beta1/_helpers.py

这个实现在过去对我有用,但有几天我无法让它成功。

import firebase_admin 
from firebase_admin import credentials 
from firebase_admin import firestore 
from firebase_admin import db     
from google.cloud.firestore_v1beta1 import ArrayUnion, ArrayRemove 
 
class TwitFire: 
 
    def __init__(self): 
       # Use a service account 
       cred = credentials.Certificate('./credentials') 
       firebase_admin.initialize_app(cred) 
       self.client = firestore.client() 
 
    def getRef(self, collectionName, documentName): 
        return self.client.collection(collectionName).document(documentName) 
 
    def set(self, ref, entry): 
        return ref.set(entry) 
 
    def update(self, ref, entry): 
        return ref.update(entry) 
 
    def updateTweets(self, ref, entry): 
        return ref.update({u'retweets': ArrayUnion([entry])}) 
 
    def create(self, ref, entry): 
        return ref.create(entry) 

请您参考如下方法:

你在使用 google-cloud-firestore==0.32.1 吗?

当我升级到 32.1 时遇到同样的问题。

降级到 google-cloud-firestore==0.31.0 并且问题消失了。 可能是最新版本中的错误。

注意我本地没有测试,只是改了requirements.txt

编辑:

from google.cloud.firestore_v1beta1 import ArrayUnion 

应该与 0.31.0 一起使用 当使用 0.32.0 及更高版本时,导入必须是:

from google.cloud.firestore_v1 import ArrayUnion 

参见 this