import firebase_admin
from firebase_admin import credentials
from firebase_admin import firestore
import traceback
import json
cred = credentials.Certificate(constants.path_to_json)
default_app = firebase_admin.initialize_app(cred)
other_app = firebase_admin.initialize_app(cred, name='other')
db = firestore.client()
match_id = 'blala'
pool_id = 'pidxyz'
cont_team_id = '229'
doc_ref = db.collection('match-pools')\
.document(match_id)\
.collection('pid')\
.document(pool_id)\
.collection('contestant-teams')\
.document(cont_team_id)
def create_counter(ref, num_shards):
batch = db.batch()
print(batch)
# Initialize the counter document
batch.set(ref, { 'num_shards': num_shards })
# Initialize each shard with count=0
for i in range(0, num_shards):
shardRef = ref.collection('shards').document(str(i))
batch.set(shardRef, { count: 0 })
# Commit the write batch
return batch.commit()
create_counter(doc_ref, 5)
AttributeError: 'CollectionReference' object has no attribute 'doc'
Method name is document()
, not ``doc(). You use it correctly when initializing doc_ref
, but create_counter()
method doesn't do it right.
Copy of comment by Hiranya Jayathilaka since it is the correct answer. OP, you can accept this comment to close the question.
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.