通过ArrayUnion

时间:2019-04-15 14:14:07

标签: python python-3.x google-cloud-firestore firebase-storage firebase-admin

我想通过创建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)

1 个答案:

答案 0 :(得分:1)

Are you using google-cloud-firestore==0.32.1?

Experienced the same issue when I upgraded to 32.1.

Downgraded to google-cloud-firestore==0.31.0 and the issue is gone. Maybe a bug in the recent version.

Note that I did not tested locally, just changed the requirements.txt

EDIT:

from google.cloud.firestore_v1beta1 import ArrayUnion

should be used with 0.31.0 when using 0.32.0 and above the import must be:

from google.cloud.firestore_v1 import ArrayUnion

See this