AttributeError:“ CollectionReference”对象没有属性“ doc”

时间:2019-04-24 06:57:14

标签: python google-cloud-firestore firebase-admin

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”对象没有属性“ doc”

1 个答案:

答案 0 :(得分:0)

方法名称为document(),而不是doc()。初始化doc_ref时可以正确使用它,但是create_counter()方法不能正确使用。

Hiranya Jayathilaka发表的评论的副本,因为它是正确的答案。 OP,您可以接受此评论以关闭问题。